5

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; }
}
gdario
  • 231
  • 2
  • 3
  • 5
  • possible duplicate of [What is the difference between a field and a property in C#?](http://stackoverflow.com/questions/295104/what-is-the-difference-between-a-field-and-a-property-in-c) – nawfal Jun 03 '13 at 18:26

4 Answers4

23

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.

ChrisF
  • 134,786
  • 31
  • 255
  • 325
  • 7
    I think 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. – snarf Nov 07 '09 at 18:11
  • @Snarfblam - that's what I was trying to say, you don't mind if I steal it? – ChrisF Nov 07 '09 at 18:16
  • @Snarfblam, you often can change it without breaking code, but you can break existing executables and libraries which depend on it. Those need recompilation. – Dykam Nov 07 '09 at 18:19
  • 1
    Keep in mind that properties have other advantages over fields. Some Attributes can only be placed on properties and not on fields. And there are some (at least used to be) databinding scenarios that won't work with fields. – Rob Nov 07 '09 at 18:31
3

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.

Letterman
  • 4,076
  • 5
  • 29
  • 41
1

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.

Dykam
  • 10,190
  • 4
  • 27
  • 32
venezia
  • 214
  • 1
  • 2
  • that's not answering the question – Letterman Nov 07 '09 at 18:42
  • I know that's not answering the question because the first one (ChrisF) answers well enough the difference. I only wanted add a precisation about the concise syntax of the property. – venezia Nov 08 '09 at 16:42
0

The answer is, yes you can remove the { get; set; } but then a whole load subtle differences kick in. Some will say fields and properties express radically different design intent but in practice this distinction has been eroded over the years as C# evolves and progressively blurs the the syntactic differences.

For a good list of compiler-binary level differences between fields and properties refer to SO question difference-between-property-and-field-in-c. But the answers to that question missed one significant point about the special role of properties when declaring interfaces.

Community
  • 1
  • 1
camelCase
  • 195
  • 8