Possible Duplicate:
What is the difference between a field and a property in C#
Can someone explain the diffrence if any between these two properties?
public string City { get; set; }
public string City;
The first one is an actual property. The second one is just a field.
Generally speaking, fields should be kept private
and are what store actual data. Properties don't actually store any data, but they point to fields. In the case of the auto-property above, it will auto-generate a hidden field like _city behind the scenes to hold the data.
Hope this helps!
First one is CLR property, while the second is just public field (not a property).
In WPF and Silverlight, binding doesn't work with public fields, it works only with public properties. That is one major difference in my opinion:
//<!--Assume Field is a public field, and Property is a public property-->
<TextBlock Text="{Binding Field}"/>
<TextBlock Text="{Binding Property}"/>
First one wouldn't work but the second one would work.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With