tags:

views:

946

answers:

7

Possible Duplicate:
C#: Public Fields versus Automatic Properties

Duplicate? I think not:
This question is not the same as "Why use properties instead of public field". A property with a specified getter and setter is far different than a public field. My question was, is a property WITHOUT a getter and setter, any different.

With the somewhat recent ability to have empty getters and setters, what is the benefit of using them instead of just declaring a public member variable?

Example:

public string MyProperty
{
    get;
    set;
}

versus:

public string MyProperty;
+8  A: 

One word: inheritance.

Properties are inheritable while fields are not. You can use fields in an inherited class, but not alter their behavior by making them virtual.

Like so:

public class Foo {
  public virtual int MyField = 1; // Nope, this can't

  public virtual int Bar {get; set; }
}

public class MyDerive : Foo {
  public override MyField; // Nope, this can't

  public override int Bar {
    get {
      //do something;
    }
    set; }
}

Edit: Besides the fact of inheritance, the points pointed out in the other answers (like visibility) are also a huge benefit of properties over fields.

Webleeuw
In what context are public fields not inherited by a derived class? Or am I misunderstanding you?
Dave
You can make them virtual and override them in derived classes.
Oliver Hanappi
Something about this scares me. It seems odd to be overriding an auto-property. Since under the hood the framework is spinning up a private member to actually store the value of the property it seems as though by overriding it you might do something unexpected. I'd be inclined to mark autoproperties as not overrideable. I can't quite put my finger on it, but it just seems...odd.
Chris Clark
Why should it be scary? Consider the use of abstract classes with virtual (not necessarily abstract) auto-implemented properties. It would be a pity if those weren't overridable imho.
Webleeuw
+6  A: 

One thing you can do with properties that you can't do with fields is limit visibility for either setter or getter:

public string MyProperty { get; private set; }

Something I use quite a lot.

And something (more powerful) you can't do with fields is define them inside an interface. Suppose you want an interface that requires implementing classes to have a certain property:

public interface MyInterface
{
    string MyProperty { get; }
}

Note that you do not need to have a setter here. It is entirely up to implementing classes to determine how they should set MyProperty.

Ronald Wildenberg
You beat me :-). Have separate access modifiers even if the implementation is empty.
SKG
It may be worth to point out that you can do something quite similar (although not half as powerful) by declaring your field as readonly, by doing that it can only be assigned to at construction time.
Patrik Hägne
+5  A: 

Fields cannot be exposed in interfaces. And the auto-property can be changed into a "normal" property at any time if needed, without having the signature and interface of the class changing.

In general, fields are considered to be an implementation detail, which may change in future versions of the code. Therefore, you should expose data via methods and properties, leaving the way open for internal changes in the future which do not affect code using the class.

Lucero
Good answer, to clarify: changing a field to a property is actually a breaking change as it does not preserve binary compatibility. One example would be that some user of the class may be doing some reflection upon it and a FieldInfo is something completely different than a PropertyInfo. Other example are mentioned in other answers.
Patrik Hägne
Thanks for the clarification, that's what I meant to say. ;)
Lucero
+1  A: 

The idea is to manage the values inside of the object, state, avoiding corruption and misuse by calling code.

AmiT
+2  A: 

A property gives you several advantages over a simple public field:

  • you can control whether the property is read-only, write-only, or read/write
  • you can hide the actual implementation (maybe in the setter you want to do more than just setting a value)
  • when using databinding (e.g. in ASP.NET), you'll have to use properties (does not work with fields)
M4N
A: 

You can flag properties with attributes that aren't available on members. There are attributes that only apply to fields in the DataContract namespace that affects serialization, the attribute can't be applied to fields, etc.

Admittedly, there isn't anything technically preventing these attributes from being used on members, but nonetheless they only work on properties.

Chris Clark
+3  A: 

Tight coupling comes to mind. Using public fields removes the layer of abstraction made available by the use of properties. Using private fields and properties hides the implementation from other classes and helps to insulate them (external classes) whenever a change is necessary.

Also, keep in mind that you are referring to auto-implemented properties which causes the compiler to create the backing field for you instead of you having to manually create the backing (private) field for each property on your class.

Wil P
Yes. Hide your privates!
Jeremy McGee
Hahahaha - never heard it that way :) Great! +1 from me.
Wil P