C
C#7d ago
Faker

OOP - Properties and Fields

Hello guys, sorry to disturb you all, consider the following code:
C#
namespace Classes;

public class BankAccount
{
public string Number { get; }
public string Owner { get; set; }
public decimal Balance { get; }

public void MakeDeposit(decimal amount, DateTime date, string note)
{
}

public void MakeWithdrawal(decimal amount, DateTime date, string note)
{
}
}
C#
namespace Classes;

public class BankAccount
{
public string Number { get; }
public string Owner { get; set; }
public decimal Balance { get; }

public void MakeDeposit(decimal amount, DateTime date, string note)
{
}

public void MakeWithdrawal(decimal amount, DateTime date, string note)
{
}
}
Noticed that we didn't defined the fields but only the properties which is used to get and set the variable. I have some questions. We didn't declare any fields or constructors, are the fields created automatically behind the scenes? For the constructor, we will be using the default constructor if nothing is provided?
13 Replies
Faker
FakerOP7d ago
If I wanted to use conditions in the properties, like Owner should only contain alphabets, how can I do so pls here we would need to explicitly define the field?
Sossenbinder
Sossenbinder7d ago
For properties, backing fields will automatically be generated, yes
Faker
FakerOP7d ago
yep I see, this is valid only if I don't modify them to add certain conditions ?
Sossenbinder
Sossenbinder7d ago
If you don't specify a custom constructor an implicit default constructor will be provided Oh, yeah, if you want to modify the behaviour of the getter or setter you'll have to do some work yourself There's an ongoing proposal / implementation to add a field keyword But it's not quite there yet https://learn.microsoft.com/en-us/dotnet/csharp/programming-guide/classes-and-structs/properties#field-backed-properties
Faker
FakerOP7d ago
thanks, will have a look !
Sossenbinder
Sossenbinder7d ago
Yw! Regarding your other question, if you want to add conditions to the properties, you could e.g. modify the setter to check the assigned value
Faker
FakerOP7d ago
yeah, I should follow the following syntax:
C#
public class Person
{
public string? FirstName
{
get;
set => field = value.Trim();
}

// Omitted for brevity.
}
C#
public class Person
{
public string? FirstName
{
get;
set => field = value.Trim();
}

// Omitted for brevity.
}
For the getter and setters ?
Sossenbinder
Sossenbinder7d ago
I think for the time being you'd be better off with using explicitly declared fields like showcased in https://learn.microsoft.com/en-us/dotnet/csharp/programming-guide/classes-and-structs/using-properties
Using Properties - C#
These examples illustrate using properties in C#. See how the get and set accessors implement read and write access and find out about uses for properties.
Sossenbinder
Sossenbinder7d ago
The field keyword requires you to turn on some language preview settings
Faker
FakerOP7d ago
yep I see, will just create a class, add some stuff and came back
C#
public string Name()
{
get {
return _name;
}
set
{
_name = value;
}
}
C#
public string Name()
{
get {
return _name;
}
set
{
_name = value;
}
}
Can someone explain why is the code above wrong please, my IDE is telling me get, set and value can't be resolved
Sossenbinder
Sossenbinder7d ago
The () parentheses are not valid syntax for properties Just remove them
Faker
FakerOP7d ago
ahh yeah true sorry

Did you find this page helpful?