Does an uppercase property auto-create a lowercase private property in C#?

Why not just have a look what's going on?

  public class Test {
    // private int myProp;

    public int MyProp {
      get;
      set;
    }
  }

...

  string report = String.Join(Environment.NewLine, typeof(Test)
    .GetFields(BindingFlags.NonPublic | BindingFlags.Instance)
    .Select(field => field.Name));

  Console.Write(report);

And you'll get quite a weird name

<MyProp>k__BackingField

however this strange backing field name ensures that there'll be no name conflict (you can't declare any field, property, method etc. starting with <).

Edit: if you uncomment // private int myProp; line you'll have

myProp
<MyProp>k__BackingField

please, notice that myProp is not a backing field for the MyProp property.


The casing has nothing to do with it.

Writing a property like below

public int x { get; set; }

will always create and use an anonymous private field which it manipulates via get/set.


The compiler does create a private backing-field of the desired type for you, however with another name (it´s not simply x as in your example, it´s more something like <X>_BackingField). Thus you can´t access the field. However the actual syntax is quite similar.

Have a further look at auto-generated properties:

the compiler creates a private, anonymous backing field that can only be accessed through the property's get and set accessors.

Having said this the two code-samples within your question are identical in their meaning.

Tags:

C#

Properties