SSW Foursquare

Do you use Public/Protected Properties instead of Public/Protected Fields?

Last updated by Tiago Araújo [SSW] almost 2 years ago.See history

Public/Protected properties have a number of advantages over public/protected fields:

  • Data validation
    Data validation can be performed in the get/set accessors of a public property. This is especially important when working with the Visual Studio .NET Designer.
  • Increased flexibility
    Properties conceal the data storage mechanism from the user, resulting in less broken code when the class is upgraded. Properties are a recommended object-oriented practice for this reason.
  • Compatibility with data binding
    You can only bind to a public property, not a field.
  • Minimal performance overhead
    The performance overhead for public properties is trivial. In some situations, public fields can actually have inferior performance to public properties.
public int Count;

Figure: Bad code - Variable declared as a Field

public int Count
{
 get
 {
 return _count;
 }
 set
 {
 _count = value;
 }
}

Figure: Good code - Variable declared as a Property

We agree that the syntax is tedious and think Microsoft should improve this.

Adam Cogan
We open source.Loving SSW Rules? Star us on GitHub. Star
Stand by... we're migrating this site to TinaCMS