Can someone tell me, what is the porpose of using a public property for a private class member but not implementing anything in the set and get part? I know that it could be just for examples and that later on you can implement something but I don't know if there is any meaning using it like that
-
That could be an auto-implemented property. Depends on what you mean by empty and whether it is VB or C# – Ňɏssa Pøngjǣrdenlarp Dec 11 '16 at 21:44
-
Are you referring to auto properties? i.e., public int Foo { get; set;} – JTW Dec 11 '16 at 21:44
-
@MichalHainc I do not believe it is a duplicate of that question. Read his question carefully and see my answer. – CodingYoshi Dec 11 '16 at 22:13
-
@MichalHainc i appreciate that you want to clear all duplicate questions, but i was not satisfied whit the answers that i found, sorry for not reading everything – wookiee Dec 12 '16 at 09:08
3 Answers
I am going to assume you know there is a private field generated by the C# compiler as the field backing up this property. It is syntactic sugar. I am also going to assume you know this is an auto-implemented property. So what is the point of a property if it public with no logic in the get
or set
. In other words, any class can access it and set its value. So why choose this property over a public field?
Purpose of it is:
- Today the
get
andset
is empty but it may need code in the future. To avoid breaking a contract in the future, you use an emptyget
andset
. - Most .NET databinding can be done against properties but not fields. So although it is empty, it still serves a far greater purpose.
- The
get
andset
can have access modifiers. For example, you can sayset
is private butget
is public. - A field cannot be used in interfaces but properties can.

- 25,467
- 4
- 62
- 64
This is an "auto-implemented" property in C#. In effect, the property acts as a full property, i.e.:
This:
public int Number { get; set; }
Is equivalent to:
private int _number;
public int Number
{
get
{
return this._number;
}
set
{
this._number = value;
}
}
This prevents you from having to type out the full property declaration. This is useful when you don't have any additional logic in the getter and/or setter.
If you're creating simple data mapping and transfer classes, auto-implementing properties can make the class definition far more concise.

- 3,546
- 8
- 35
- 49
When you see
public int Count { get; set; }
as a member of a class, that is not nothing. You're just deferring to the default implementation of auto-properties. It's roughly,
private int _count;
public int get__Count()
{
return _count;
}
public void set__Count(int value)
{
_count = value;
}
You just have syntactic sugar in C# to use these methods roughly like a field, i.e.
instanceOfObject.Count = 42; // Not a field access, but behind the scenes a method call.

- 15,022
- 3
- 41
- 74
-
@MichalHainc That's a fair opinion. If you think my answer is not useful or doesn't not contribute to Stack Overflow, you're welcome to down vote it. – jdphenix Dec 11 '16 at 21:54