Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C#: Can I remove "{ get; set; }"?

Tags:

c#

Is there a difference between:

public T RequestedValue { get; set; }

and

public T RequestedValue;

?

Taken from this code:

public class PropertyChangeRequestEventArgs<T>:EventArgs
{
    public PropertyChangeRequestEventArgs(T pRequestedValue)
    {
        RequestedValue = pRequestedValue;
    }

    public T RequestedValue { get; set; }
}
like image 353
gdario Avatar asked Nov 07 '09 17:11

gdario


3 Answers

The first is an Auto-Implemented Property the second is a Field. Regular Properties expose Getters and Setters but have a private field to actually store the value:

private int someProperty;
public int SomeProperty
{
    get { return someProperty; }
    set { someProperty = value; }
}

The first allows you to change certain aspects of the implementation of your class without affecting all the other code in your application. The most important point is that, with properties, changes can be made without breaking binary compatibility (although a field can often be changed to a property without breaking code). If it is a public member, a property is advisable. (Stealing shamelessly from Snarfblam's comment)

From the Properties page:

Properties are members that provide a flexible mechanism to read, write, or compute the values of private fields. Properties can be used as if they are public data members, but they are actually special methods called accessors. This enables data to be accessed easily and still helps promote the safety and flexibility of methods.

Properties with a backing field are the most flexible form as they allow easy implementation of things like the INotifyPropertyChanged event for updating the UI in Model-View-ViewModel implementations.

like image 81
ChrisF Avatar answered Sep 24 '22 21:09

ChrisF


deep explanation!

The {get; set;} is an automatic property, while the second is a field.

a field is a normal variable, from some type, that contains data.

a property is a couple of methods (well sometimes it's just one), one for get, and one for set. they only have a syntax like fields, but actually they are quite different.

properties are usually for filtering the set of the value, or virtualizing something in the get, etc.

automatic properties, also create a private field behind the scenes, return its value in the get, and set its value in the set.

seemingly this is just like a normal field, but behind the scenes (IL) using properties is totally different from using fields.

 a.Property1 = 4;

is translate into something like:

 a.Set_Propert1(4);

and this:

x = a.Property1;

is translate to something like this:

x = a.Get_Property1();

so why is it a good practice to use only public properties, even if they are automatic?

say you are writing a library, that is used by other application, and someday you want to release a new version of that library that constrains one of your class' fields..

if you are using properties, you can just change the property (even if it is an automatic one, you can replace it by a full one), and then any application which used your library can still use it in the same way.

but if you made a public field, which you now want to constrain, you'll need to make a property for this and make the field private, but if you will, any application that used you library will no more be bale to, because the way it use fields and property is different.

like image 45
Letterman Avatar answered Sep 24 '22 21:09

Letterman


You may write:

public T RequestedValue { get; set; }

as a shortcut of:

private T _requestedValue;

public T RequestedValue
{
  get { return this._requestedValue; }
  set { this._requestedValue = value; }
}

They are totally equivalent, also considering the performance.

like image 45
venezia Avatar answered Sep 22 '22 21:09

venezia