Reputation: 617
When creating an auto-implemented property, C# generates a member for that property somewhere in the background. I for the life of me can't remember what this is called, or what the naming convention for the member is called. After Googling for a while, I thought it might be an idea to just ask.
Property:
public int Age { get; set; }
My guess (from memory) of the hidden member:
private int i_age;
Edit #1
To clarify, I was looking for the term of the auto generated member, which was answered by Dmitry Bychenko below. The term is "backing field"
Upvotes: 2
Views: 169
Reputation: 43
Hi You may want to try this.
private int i_age;
public int Age
{
get {return i_age;}
set {i_age = value;}
}
Upvotes: 0
Reputation: 186823
Why not carry out a simple experiment?
using System.Reflection;
...
public class Experiment {
public int Age { get; set; }
}
...
var fieldNames = string.Join(", ", typeof(Experiment)
.GetFields(BindingFlags.NonPublic | BindingFlags.Instance)
.Select(field => field.Name));
Console.Write(fieldNames);
Outcome:
<Age>k__BackingField
Please, notice that unlike i_age
actual field's name <Age>k__BackingField
doesn't conflict with any field (we can't declare a field with such a name)
Upvotes: 6