Tutorial :Why prefer Properties to public variables? [duplicate]



Question:

Other being able to sanity check values in a setter is there a more underlying reason to prefer properties to public variables?


Solution:1

We've had this subject before but I can't find anything now.

In brief: your needs might change: where there's no sanity check now, one might be required in the future. However, if you change your public fields to properties, this breaks binary compatiblity: every client who uses your code/library would have to re-compile.

This is bad because it potentially costs a lot of money.

Using properties from the beginning avoids this problem. This even counts for code that is not part of a library. Why? Because you never know: the code (even if highly domain-specific!) might prove useful so you want to refactor it to a library. This refactoring process is obviously made much easier if you are already using properties in place of public/protected fields.

Additionally, writing public properties is easy in C# 3.0 because you can just use the auto-implemented properties, saving you quite a bit of code:

public DataType MyProperty { get; set; }  

Will implement the necessary backing field and getter/setter code for you.

I will add a personal note: .NET's behaviour in this regard is somewhat lazy. The compiler could just change public fields to properties on the fly, thus avoiding the problem. VB6 already did this for COM-exposed classes and I see absolutely no reason for VB.NET and C# not to do the same. Perhaps someone on the compiler teams (Jared?) could comment on this.


Solution:2

In a nutshell:

  • You can control acces (readonly,
    writeonly, read/write)
  • You can validate values when setting a property (check for null etc)
  • You can do additional processing, such as lazy initialization
  • You can change the underlying implementation. For example, a property may be backed by a member variable now, but you can change it to be backed by a DB row without breaking any user code.


Solution:3

Changing a field to a property in the future is considered a breaking change. Fields are considered implementation details of classes and exposing them publicly breaks encapsulation.


Solution:4

Jeff Atwood has blogged about it:

There are valid reasons to make a trivial property, exactly as depicted above:

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

It's a shame there's so much meaningless friction between variables and properties; most of the time they do the exact same thing. Kevin Dente proposed a bit of new syntax that would give us the best of both worlds:

public property int Name;  

However, if the distinction between variable and property is such an ongoing problem, I wonder if a more radical solution is in order. Couldn't we ditch variables entirely in favor of properties? Don't properties do exactly the same thing as variables, but with better granular control over visibility?


Solution:5

If you work in a closed environment -- you dont develop a SDK, all classes are used within a same project framework -- there is no difference.

The usual argument is that "in the future you may need to do some check on the values, so it is easier with properties". I dont buy it at all.

Using public fields is more readable, less decoration and easier to use.


Solution:6

You can also protect write access and allow read access with a property:

public int Version { get; private set; }  


Solution:7

Use of properties makes your code more object oriented. By making member variables public, you are exposing your implementation.

Also see this link from C#'s Programming Guide


Solution:8

Yes.

Consider a public varibale which now holds a string, you can simply set it. However, if you decide that that public variable should hold an object which should be initialized with a string then you would have to change all your code using your original object. But if you would have used setter you would only have to change the setter to initialize the object with the provided string.


Note:If u also have question or solution just comment us below or mail us on toontricks1994@gmail.com
Previous
Next Post »