Since C# 3.0 there is the new syntax to autogenerate the instance variable from a propertie:
public string Foo { get; set; }
But there is no way to access the underlying backing field. So I don't really see the point of it because declaring the same instance variable would produce the same effect without the overhead of calling the "getter" and "setter".
public string Foo;
Declaring the property like:
public string Foo { get; }
or
public string Foo { set; }
is totally useless since we are not able to write resp. read the content of the field.
Does someone have a good explanation for why in C# they have put all this effort to introduce this syntactic sugar?
EDIT: People seems to think that I am confusing field and properties so let me clarify what I have said.
If you are using a property with auto generate field:
public string Foo { get; set; }
Then there is no point to it, since whenever you access the properties there is the overhead call to the get_Foo()
"getter" every time you are accessing the properties and since your aren't doing anything particular in the the "getter" there is not mush interest of creating this property. Creating the field would have been exactly the same and it is mush faster (optimization wise).
Thanks
By itself it's pointless, but you can do things like this:
public string Foo { private set; get; }
Which has a lot more value.
Also properties and fields are not the same things.