Marc (34) [Avatar] Offline
I know you have proofreaders.
This might fall somewhere between a bug and my lack of understanding of C# and a typo.
1. Is the compiler auto-creating a private backing field named "name"?
2. It appears that the value of the name is being stored in a member variable in the counter object (
3. The 'if' statement appears to be checking the incoming value against the auto-created private backing field, not the value in the counter object?
4. In addition to the 'magic' of auto-creating a backing field, does the compiler also automatically update that field in the setter method, regardless of any other code we write? That is, both and name are kept in sync?

public string Name
get { return counter.Name; }
if (Name == value) return;
counter.Name = value;
Jim Bennett (88) [Avatar] Offline
In C# all properties are methods - under the hood the IL created is two methods, get_Property() and set_Property().
If you create an auto-property without providing the body, such as:

public string Name {get; set;}

Then under the hood a backing field is created and the get and set methods are created for you, so the resulting IL would be the same as:

private string _name_field_with_a_unique_name;
public string get_Name() { return _name_field_with_a_unique_name; }
public void set_Name(string value) { _name_field_with_a_unique_name = value; } 

However, if you provide an implementation then there is no autogenerated backing field, and the get and set methods are the ones you provide, not autogenerated methods. In the code you provided, there is no autogenerated field, no autogenerated methods. The get method just returns the value of counter.Name, the sea method just sets counter.Name. There is no 'name' field created for you.