C
C#4d ago
Faker

Properties vs Fields in C#

Hello guys, can someone explain what is the difference between Properties and Fields in C# please. At first I thought that properties and fields were the same thing because they are accessed the same way, like writing person.name something like that. But this isn't the case. How do properties work? I noticed they work as method but they don't have any parentheses. They should be named the same way as the fields?
c#
public class Person
{
private int age; // Private field

// Property: Combines getter and setter
public int Age
{
get { return age; } // Getter
set
{
if (value >= 0) // Validation
age = value; // Setter
}
}
}
c#
public class Person
{
private int age; // Private field

// Property: Combines getter and setter
public int Age
{
get { return age; } // Getter
set
{
if (value >= 0) // Validation
age = value; // Setter
}
}
}
12 Replies
Angius
Angius4d ago
$propvsfield
MODiX
MODiX4d ago
Why use properties over a public field? - Properties can individually specify access modifiers for get and set - With Visual Studio, you can type prop, press tab and it will auto complete for you - XAML requires properties for bindings - Field exposure is really only done in readonly structs - Using properties allows for future changes to the getter or setter without breaking your API/external programs (binary compatibility) Example of an auto property:
class Example
{
public string Name { get; set; }
public int Age { get; set; }
}
class Example
{
public string Name { get; set; }
public int Age { get; set; }
}
Example of a property with backing field:
class Example
{
private string _name;
public string Name
{
get { return _name; }
set { _name = value; }
}

private int _age;
public int Age
{
get { return _age; }
set { _age = value; }
}
}
class Example
{
private string _name;
public string Name
{
get { return _name; }
set { _name = value; }
}

private int _age;
public int Age
{
get { return _age; }
set { _age = value; }
}
}
Angius
Angius4d ago
They don't have to be named the same as the field. They don't even need to be tied to a field, necessarily For example, you could have
public string FirstName { get; set; }
public string LastName { get; set; }
public string FullName => $"{FirstName} {LastName}";
public string FirstName { get; set; }
public string LastName { get; set; }
public string FullName => $"{FirstName} {LastName}";
or
public int Age { get; set; }
public bool IsAdult => Age >= 18;
public int Age { get; set; }
public bool IsAdult => Age >= 18;
Faker
FakerOP4d ago
yep I see, but one thing... the properties acts as method, why they don't have parentheses ? I mean how the compiler know this isn't a field
Jimmacle
Jimmacle4d ago
because the compiler understands the syntax used to define properties, namely the { } with get and/or set inside
Faker
FakerOP4d ago
ahhh I see Noted, thanks guys ! 👍
Sehra
Sehra4d ago
it's just syntactic sugar, after compiling it will be methods set_Age and get_Age, but marked to be special
Faker
FakerOP4d ago
yeah, like behind the scenes, the compiler would still process it like set_Age(int age)... ?
Sehra
Sehra4d ago
yup, and some code that says the property Age has those as set/get method
Angius
Angius4d ago
$getsetdevolve
MODiX
MODiX4d ago
class Foo
{
private int _bar;

public int GetBar()
{
return _bar;
}

public void SetBar(int bar)
{
_bar = bar;
}
}
class Foo
{
private int _bar;

public int GetBar()
{
return _bar;
}

public void SetBar(int bar)
{
_bar = bar;
}
}
can be shortened to
class Foo
{
private int _bar;

public int GetBar() => _bar;

public void SetBar(int bar) => _bar = bar;
}
class Foo
{
private int _bar;

public int GetBar() => _bar;

public void SetBar(int bar) => _bar = bar;
}
can be shortened to
class Foo
{
private int _bar;
public int Bar {
get { return _bar; }
set { _bar = value; }
}
}
class Foo
{
private int _bar;
public int Bar {
get { return _bar; }
set { _bar = value; }
}
}
can be shortened to
class Foo
{
private int _bar;
public int Bar {
get => _bar;
set => _bar = value;
}
}
class Foo
{
private int _bar;
public int Bar {
get => _bar;
set => _bar = value;
}
}
can be shortened to
class Foo
{
public int Bar { get; set; }
}
class Foo
{
public int Bar { get; set; }
}
Angius
Angius4d ago
The compiler would produce something like the first example from any autoproperty

Did you find this page helpful?