tags:

views:

1235

answers:

11

Has anyone else seen people do this:

private string _name;
public string Name{ get{ return _name; } set{ _name = value;}}

I understand using accessors if you are going to exercise some sort of control over how it gets set or perform some sort of function on it when there is a get. But if you are just going to do this, why not just make the variable public to begin with? Am I missing something?

A: 

Because you will have to refactor it anyway later, when you suddenly need that control.

GSerg
A is typically "ain't" for things that take time and effort to do, and "are" for things which don't. This doesn't.
Steve Jessop
Although that said, a change from a get/set which can't fail to one which can is pretty traumatic, so this still only gives you limited scope for change. But it's basically free, so why not?
Steve Jessop
A: 

Preparation. You never know when you'll want to removed the set accessor down the road, perform additional operations in the setter, or change the data source for the get.

marc
A: 

Publicly accessible members should typically be methods and not fields. It's just good practice, and that practice helps you ensure that the encapsulated state of your objects is always under your control.

Justice
+5  A: 

The idea is that if you use accessors, the underlying implementation can be changed without changing the API. For example, if you decide that when you set the name, you also need to update a text box, or another variable, none of your client code would have to change.

AdamC
+10  A: 

If you define a public interface with a property in assembly A, you could then use this interface in assembly B.

Now, you can change the property's implementation (maybe fetching the value from a database instead of storing it in a field). Then you can recompile assembly A, and replace an older one. Assembly B would carry on fine because the interface wouldn't have changed.

However, if you'd started off initially with a public field, and decided this wasn't suitable and wanted to change the implementation and to do that you needed to convert it to a property, then this would mean you'd have to change assembly A's public interface. Any clients of that interface (including assembly B) would also have to be recompiled and replaced to be able to work with this new interface.

So, you're better off starting with a property initially. This encapsulates the implementation of the property, leaving you free to change it in the future without having to worry what clients (including assembly B) are already out in the world using assembly A. Because, if there are any clients already out in the world making use of assembly A, changing the interface would break all clients. If they're used by another team in your company, or another company, then they are going to be not happy if you break their assemblies by changing the interface of yours!

Scott Langham
+2  A: 

Good programming practice. This is a very common pattern that fits with OO design methodologies. By exposing a public field you expose the internals of how that data is being stored. Using a public property instead allows you more flexibility to change the way the data is stored internally and not break the public interface. It also allows you more control over what happens when the data is accessed (lazy initialization, null checks, etc.)

Scott Dorman
+27  A: 

If you make the member a public field, then you can't later refactor it into a property without changing the interface to your class. If you expose it as a property from the very beginning, you can make whatever changes to the property accessor functions that you need and the class's interface remains unchanged.

Note that as of .Net 3.0, you can implement a property without creating a backing field, e.g.:

public string Name { get; set; }

This removes what is pretty much the only justification for not implementing public fields as properties in the first place.

Robert Rossney
Slight correction (though I agree with the general thrust!) - automatically implemented properties are part of C# 3.0, not .NET 3.0. It's worth being clear about the difference between language and framework.
Jon Skeet
Yeah. As a VB.net user I'm saddened that we don't get goodies like this.
tom.dietrich
A: 

Variables are part of the implementation of a class. Properties more logically represent the interface to it. With C# 3.0, automatically implemented properties make this a breeze to do from the start.

I've written more thoughts on this, including the various ways in which changing from a variable to a property breaks not just binary compatibility but also source compatibility, in an article on the topic.

Jon Skeet
A: 

For encapsulation, it is not recommended to use public fields.

http://my.safaribooksonline.com/9780321578815/ch05lev1sec5?displaygrbooks=0

As Chris Anderson said later in this book, it would be ideal would be if the caller were blind to the difference of field vs. property.

Hafthor
A: 

To retain a high degree of extensibility without the pain of re-compiling all your assemblies, you want to use public properties as accessors. By following a "contract" or a defined mechanism that describes how your objects will exchange data a set of rules will be put in place. This contract is enforced with an interface and fulfilled by the getters and setters of your class that inherits this interface.

Later on, should you create additional classes from that interface, you have flexibility of adhering to the contract with the use of the properties, but since you are providing the data via the getters and setters, the implementation or process of assembling data can anything you want, as along as it returns the type that the "contract" expects.

David Robbins
+2  A: 

It might be worth noting that DataBinding in .NET also refuses to work off public fields and demands properties. So that might be another reason.

Quibblesome