19

I failed to understand why auto implemented property language feature exist in C# 3.0.

What the difference it is making when you say

public string FirstName;

than

public string FirstName { get; set; }
Soner Gönül
  • 97,193
  • 102
  • 206
  • 364
123Developer
  • 1,463
  • 3
  • 17
  • 24
  • Nobody outside of Microsoft uses the code name once the product goes RTM. – John Saunders Aug 01 '09 at 16:36
  • possible duplicate of [Difference between Property and Field in C# 3.0+](http://stackoverflow.com/questions/653536/difference-between-property-and-field-in-c-sharp-3-0) – nawfal Jun 03 '13 at 17:19
  • All the answers here seem to me to point to the fact that this is s a flaw in language design. Surely the issues here could be solved at the compiler or runtime level. Why does it have to break binary and source compatibility? Why are we being forced to add boilerplate that could be optimized away? – Andy Baker Jun 18 '17 at 14:41

7 Answers7

21

Because they are implemented differently in the resulting IL code (and machine language). An Automatic property is still exposed as a public getter and setter, whereas a public field is just that - a single field..

Thus, implementing an auto property allows you at some later date to change the internal behavior of either the getter or setter (like adding a validator) without recompiling or re=coding any dependant classes that use it...

Charles Bretana
  • 143,358
  • 22
  • 150
  • 216
10

Just to add to what other people have said, declaring a public field, the field is accessible for read and write. declaring public automatic property, although the property is public, you can still add modifier to control the accessibility at the get/set level.

public string FirstName { get; private set; }

The user of your class sees FirstName as public property. However, he/she cannot write to it.

tranmq
  • 15,168
  • 3
  • 31
  • 27
6

Consider what happens if you later want to change each of them to a property with a custom implementation. If it's an automatically implemented property, you just add a field and change the implementation. Full source and binary compatibility.

If it's a field to start with, you get neither source nor binary compatibility. You have to rebuild everything that references it, and fix up anything which no longer compiles.

Additionally, properties have various benefits over fields. My main personal objection to fields is that it exposes an implementation decision in the API.

Jon Skeet
  • 1,421,763
  • 867
  • 9,128
  • 9,194
2

The difference is that other assemblies compiled with code that read the property is compiled against a property.

If you later on decide you need to add code to the getter or setter, you can do that, without having to force every other assembly linked to it to recompile.

Not so with fields. If you later on change a field to be a property, in order to add that code, other assemblies linked against yours will cease to function properly since they're compiled to read a field, not a property.

Also, lots of code is written to find properties, not fields, like data binding and similar.

Lasse V. Karlsen
  • 380,855
  • 102
  • 628
  • 825
2

because of this use:
public string FirstName { get; private set; }
easy property, that 'kosher' by OO rules

Avram
  • 4,267
  • 33
  • 40
0

The first is a public field, while the second is a public property.

The main difference is in how they are used. For example WPF can only data bind to properties, not fields.

Cameron MacFarland
  • 70,676
  • 20
  • 104
  • 133
0

Auto properties are Compiler generated regular properties, they use a backing fields like any regular property but you don't need to write the code for that. Here is a very illustrative sample (thanks to Reflector) of the code generated by the compiler:

[CompilerGenerated]
private string <ContentType>k__BackingField;

public string ContentType
{
    [CompilerGenerated]
    get
    {
        return this.<ContentType>k__BackingField;
    }
    [CompilerGenerated]
    set
    {
        this.<ContentType>k__BackingField = value;
    }
}
Ariel Popovsky
  • 4,787
  • 2
  • 28
  • 30