Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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!

like image 823
Callum Rogers Avatar asked Aug 14 '09 12:08

Callum Rogers


People also ask

Should fields be public or private?

Fields should be declared private unless there is a good reason for not doing so. One of the guiding principles of lasting value in programming is "Minimize ripple effects by keeping secrets." When a field is private , the caller cannot usually get inappropriate direct access to the field.

Should I use fields or properties?

You should always use properties where possible. They abstract direct access to the field (which is created for you if you don't create one). Even if the property does nothing other than setting a value, it can protect you later on.

Why we use properties instead of public variables?

Property always a better choice instead of public variables. Property is safe while public variables are unsafe. And you can not debug with public variables but you can do that with property. Public variables are useful.

When should you use properties?

Consider using a property if the member represents a logical attribute of the type. Do use a property, rather than a method, if the value of the property is stored in the process memory and the property would just provide access to the value.


2 Answers

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.
like image 130
Johnno Nolan Avatar answered Sep 29 '22 10:09

Johnno Nolan


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).

like image 44
Max Schmeling Avatar answered Sep 29 '22 09:09

Max Schmeling