tags:

views:

282

answers:

8

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

+7  A: 

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.

Konrad Rudolph
Ignoring the should question, there are many reasons why the Compiler cannot do this. There are 2 reasons that come immediately to mind. 1) names and 2) calling a property is not the same as accessing a field (performance, ref and out). Unfortunately there's not enough space here to truly comment
JaredPar
(cont) and the thread is closed. But the short version is the user is asking for one item and you're giving them a non-equal replacement. Users are often unhappy when you do that.
JaredPar
Thanks for the answer, Jared. :-)
Konrad Rudolph
I thought the same thing, but as always, structs cause problems: v.S.M = 5 assigns 5 to the member M of struct S. If S is a field, there's no problem. If it's a property, this would only modify M in a temporary copy of S returned from the property getter. Mutable structs are the root of much evil!
Daniel Earwicker
+1  A: 

Jeff has blogged about it

Konstantinos
A: 

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

Mystic
+2  A: 

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.

Mehrdad Afshari
It is only a breaking change if the calling code can not be recompiled, most of the time you own the caller code and it is compiled by the same build system. Hence this is not a problem for most peole out side of the .NET framework team.
Ian Ringrose
@Ian Ringrose: It's not completely source compatible either. For example, your code might do a `ref obj.MyField`. You cannot change `MyField` to `MyProperty` without changing the source.
Mehrdad Afshari
@Ian: It is a breaking change - http://msdn.microsoft.com/en-us/library/ms182141(VS.80).aspx
Cerebrus
A: 

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.

LeJeune
“Easier to use?” how come?
Konrad Rudolph
A: 

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.

Pim Jager
+1  A: 

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

private int Version { public get; set; }
Carra
+1  A: 

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