简体   繁体   中英

Auto-Implemented Property - signature of hidden member

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"

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)

Hi You may want to try this.

private int i_age;
public int Age 
{ 
    get {return i_age;} 
    set {i_age = value;}
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM