Should I use public properties and private fields or public fields for data?

In much of the code I have seen (on SO, thecodeproject.com and I tend to do this in my own code), I have seen public properties being created for every single private field that a class contains, even if they are the most basic type of get; set; like:

private int myInt;
public int MyInt 
{
     get { return myInt; }
     set { myInt = value }
}

My question is: how does this differ from:

public int MyInt;

and if we should use properties instead of public fields why should we use them in this specific case? (I am not talking about more complex examples where the getters and setters actually do something special or there is only one get or set (read/write only) rather than just returning/setting a value of a private field). It does not seem to add any extra encapsulation, only give a nice icon in IntelliSense and be placed in a special section in class diagrams!


Solution 1:

See this article http://blog.codinghorror.com/properties-vs-public-variables/

Specifically

  • Reflection works differently on variables vs. properties, so if you rely on reflection, it's easier to use all properties.
  • You can't databind against a variable.
  • Changing a variable to a property is a breaking change.

Solution 2:

Three reasons:

  1. You cannot override fields in subclasses like you can properties.
  2. You may eventually need a more complex getter or setter, but if it's a field, changing it would break the API.
  3. Convention. That's just the way it's done.

I'm sure there are more reasons that I'm just not thinking of.

In .Net 3.x you can use automatic properties like this:

public int Age { get; set; }

instead of the old school way with declaring your private fields yourself like this:

private int age;

public int Age
{
    get { return age; }
    set { age = value; }
}

This makes it as simple as creating a field, but without the breaking change issue (among other things).

Solution 3:

When you create private field name and a simple public property Name that actually gets and sets the name field value

public string Name
{
   get { return name; }
}

and you use this property everywhere outside your class and some day you decide that the Name property of this class will actually refer to the lastName field (or that you want to return a string "My name: "+name), you simply change the code inside the property:

public string Name
{
   get { return lastName; //return "My name: "+name; }
}

If you were using public field name everywhere in the outside code then you would have to change name to lastName everywhere you used it.

Solution 4:

Well it does make a difference. Public data can be changed without the object instance knowing about it. Using getters and setters the object is always aware that a change has been made.

Remember that encapsulating the data is only the first step towards a better structured design, it's not an end-goal in itself.

Solution 5:

You have to use properties in the following cases:

  1. When you need to serialize data in the property to some format.
  2. When you need to override properties in derived class.
  3. When you implement get and set methods with some logic. For example, when you implement Singleton pattern.
  4. When you're derived from interface, where property was declared.
  5. When you have specific issues related to Reflection.