Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>There is a difference. The first two are <a href="http://msdn.microsoft.com/en-us/library/ms173118.aspx" rel="nofollow">fields</a> and the remainder are auto-<a href="http://msdn.microsoft.com/en-us/library/x9fsa0sw%28v=VS.100%29.aspx" rel="nofollow">properties</a>.</p> <p>The second one, the compiler generates a private backing field and some boiler-plate get/set methods. These then allow you to access the properties like they were fields, but with the advantages only available to properties.</p> <p>It is always recommended to hide fields behind properties, by either making them private and writing your own property around it or using an auto-property.</p> <p>There's some advantages to properties. One being that properties can be made read-only, or even write-only, or read-only with an internal write-only, etc. Since they act just like methods, you can execute any arbitrary code inside of them. This is useful for when you need to implement things like <code>INotifyPropertyChanged</code> or if the property is actually calculated from several fields behind it.</p> <p>The other advantage is encapsulation. You aren't tying yourself directly to the fields of the class, but rather the property. So if some detail about the field changes (say it goes away and becomes calculated), by using the property you are insulating yourself from those implementation details.</p> <p>You should certainly look at using properties (for now adding the <code>{ get; set; }</code>) for all cases. They are good practice in that they provide a level of encapsulation that shields the user from implementation specific details.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload