41

Possible Duplicates:
Should I use public properties and private fields or public fields for data?
Difference between Automatic Properties and public field in C# 3.0

People seem to dogmatically insist on the use of public properties over fields but why is it so ultra-important in the case of simple properties?

How is

public int Foo { get; set; }

so incredibly different than

public int Foo;

?

Off the top of my head I can think of few practical differences between the two:

  • Accessing the member using reflection (rare, and most decent reflective algorithms will account for the difference)
  • The second entry allows you to use the field as a valid parameter for ref and out parameters, which would seem to be an advantage to using the field version
  • Fields don't work in Remoting (probably, I've never used remoting but I imagine they wouldn't)?

Other than these fairly rare cases, changing Foo to be a computed property later results in 0 lines of code changed.

5

5 Answers 5

69

Using properties has a couple of distinct advantages:

  • It allows for versioning if later you need extra logic. Adding logic to the getter or setter won't break existing code.
  • It allows data binding to work properly (most data binding frameworks don't work with fields).

In addition, there are almost no disadvantages. Simple, automatic properties like this get inlined by the JIT compiler, so there is no reason not to use them.

Also, you mentioned:

Other than these fairly rare cases, changing Foo to be a computed property later results in 0 lines of code changed.

This doesn't require your code to be changed, but it does force you to recompile all of your code. Changing from a field to a property is a breaking API change which will require any assembly which references your assembly to be recompiled. By making it an automatic property, you can just ship a new binary, and maintain API compatibility. This is the "versioning" advantage I mentioned above...

4
  • 1
    I tend to agree, the only reason you may not want to use a property is to handle ref or out requirements, although you can easily produce a temporary variable to use and then set the property to that variable which will be disposed soon after. Jul 20, 2015 at 4:50
  • 1
    Using properties is preferable to using fields because you can change the statements in the get and set blocks without needing to change the classes that depend on the property. Aug 23, 2016 at 10:45
  • If you use the set block to check the value before assigning, you make it more difficult to reason about client code: person.Name = ""; print(person) might suggest that the Name was changed, but the setter might prevent it. In summary using the "set block" seems to me to be an anti feature. Nov 16, 2016 at 15:19
  • @MichaWiedenmann that is a matter of bad coding practices. If a bad value is rejected, it should be rejected with an exception. Apr 26, 2021 at 9:29
50

One good reason is that you can vary the get/set accessibility.

public int Foo {get; protected set;}
3
  • 2
    Wow, I did not actually realise this. +1 and thanks Jan 31, 2014 at 16:17
  • Though the readonly modifier for fields that only should ever be set in the constructor addresses many cases where this would be helpful. And, also, now that it is the future, you can do public int Foo { get; } (auto-implemented get-only property backed by a readonly field).
    – binki
    Sep 23, 2016 at 15:49
  • I guess. for future changes you could make your variables properties instead of fields. but is there a good practice to define difference between variables or properties ?
    – Mike
    Jul 3, 2018 at 19:28
31

A property is a language element which logically represents a property of the thing being modeled by the class. The class Car models a car; colour is a property of cars; therefore, Color is a property of Car.

A field is an language element which represents an implementation detail of the class. Your car does not have a "colour field", so your program's representation of a car should not expose a field called Color. It might contain a private implementation detail whereby the property Color is implemented by a field, but that's a private implementation detail, not a publically accessible part of the model.

10
  • 1
    I like this explanation better, because it doesn't deny the fact that properties are in fact a redundant feature of the language, if you take this logical semantic out of the picture. The only remaining advocate in favor would be the reflection (like Reed mentions for data binding). But this can be achieved with annotations (attributes) on fields.
    – v.oddou
    Aug 24, 2015 at 9:38
  • 8
    @v.oddou: Almost every feature of the language is redundant to something. Methods, for example, are unnecessary; methods could just be fields of delegate type initialized with lambdas. Redundancy is often thought of as a bad thing but it is not at all; redundancy makes things easier to understand. As I note in the answer, having two features, properties and fields, lets us subtly communicate to the reader whether the member is semantically important or just a mechanism of the type. Aug 24, 2015 at 14:03
  • Why not start the two paragraphs with "A public field is a ...", and "A private field is a .."? What reason is then left to have properties? Nov 16, 2016 at 15:26
  • @MichaWiedenmann: Many properties cannot be replaced by a field. Nov 16, 2016 at 18:00
  • 1
    @Karol: Well, there are pros and cons. For example: C# has to be able to interoperate well with other .NET languages, and it has to be familiar to users of Java and C++. And there are scenarios where for performance reasons you want to have direct access to a field of a struct, particularly for plain-old-data structures used for interoperability with C++. And you can't pass properties by ref. So it's not that there are zero use cases for them. But notice that all those use cases I named imply that fields are mechanisms. Nov 17, 2016 at 20:33
8

Mostly because of convention.

The one solid argument for it is that if you later need to change from a field to a property, then all assemblies that reference yours will need to be recompiled.

Reflection does come into it once in a while, but very rarely. Some serialization types are based off of properties.

7

You can make properties virtual and override their implementation in derived classes. This is an important factor in many libraries that wrap your objects in generated proxy classes, e.g. the way NHibernate does to implement lazy loading. This isn't possible on fields.

2
  • 1
    this is in complete denial that fields can be wrapped in accessors mutators and obtain the same feature. This is especially true because properties do just that usually, they wrap a field. (as long as they are not auto properties). And bam, back to square one, properties are useless.
    – v.oddou
    Aug 24, 2015 at 9:41
  • @v.oddou And even technically auto properties wrap fields—just compiler-generated ones. It’s just syntax sugar!
    – binki
    Sep 23, 2016 at 15:51

Not the answer you're looking for? Browse other questions tagged or ask your own question.