Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

c#

properties

Take this code:

public int Foo {get;set;}

As opposed to this more complete manually written code:

private int x;
public int X 
{
    get { return x; }
    set { x = value; }
}

In the first example, does the compiler automatically create a private foo property?

I am not sure if this convention I have seen of having a lowercase private property masked by a public-facing uppercase one is just that, convention, or if it's actually part of the language/compiler, and you can either write it out or let it be done for you.

like image 579
temporary_user_name Avatar asked Jul 14 '16 11:07

temporary_user_name


People also ask

How to convert uppercase characters to lowercase in C?

We can write a C program to convert uppercase to lowercase without using any string manipulation library functions. The value of A in ASCII is 65, add +32 it becomes 97 which is ASCII value of a. So, All uppercase characters ASCII value is from 66 to 90 and if we add +32 in each uppercase character then it will become the lowercase character.

How to print the lower case string from uppercase using while loop?

Enter the string: WELCOME Upper Case to Lower case string is: welcome Let's consider an example to print the lowercase string from uppercase using while loop in C programming. /* use while loop to iterate the character array string until '\0' not occur. */ Enter an upper case string: CPROGRAMMING The lowercase string is: cprogramming

What are the uppercase and lowercase letters of the alphabet?

The uppercase letter is the capital letter of the alphabet. For example, capital letters are A, B, C, D, …, X, Y, Z. Similarly, a lowercase letter is represented by the small letter of the alphabet. For example, small letters are a, b, c, d, e…., w, x, y, z.

How do I use short-hand / automatic properties?

C# also provides a way to use short-hand / automatic properties, where you do not have to define the field for the property, and you only have to write get; and set; inside the property. The following example will produce the same result as the example above.


2 Answers

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.

like image 132
Dmitry Bychenko Avatar answered Oct 23 '22 22:10

Dmitry Bychenko


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.

like image 27
iuliu.net Avatar answered Oct 23 '22 22:10

iuliu.net