Properties provide controlled access to data; at its most basic, it can only mean field encapsulation (public fields are not recommended), which the compiler can do for you easily:
public int Foo {get;set;}
However, you can use the property to provide logic or handle side effects:
private int foo; public int Foo { get { return foo; } set { if(value < 0) throw new ArgumentOutOfRangeException(); if(value != foo) { foo = value; OnFooChanged();
Other common options are lazy loading, calculated elements, proxied elements, etc.
You can also change the availability, for example:
public int Foo { get; protected set; }
which can only be assigned by type and subclasses and not by unrelated code. It can also only have get or set.
In principle, the properties act as a more formal version of the get / set pair, which greatly facilitates the conversation about "Foo" rather than "get_Foo" / "set_Foo", etc. (for two-way binding).
Unlike fields, properties can also be advertised on interfaces, which is necessary for interface-based code.
Marc gravell
source share