C# 3.0 :Automatic Properties - what would be the name of private variable created by compiler

妖精的绣舞 提交于 2019-12-30 06:24:15

问题


I was checking the new features of .NET 3.5 and found that in C# 3.0, we can use

public class Person 
{    
 public string FirstName  { get; set; }
 public string LastName  { get; set; }
}

instead of

private string name;

public string Name
{
  get { return name; }
  set { name = value; }
}

If i use the Automatic Properties,what will be the private variable name for Name ? the tutorials on internet says that compiler will automatically create a private variable.So how can i use /access the private variable,if i want to use it in a method in this class ?


回答1:


Rewritten to be more clear

The field is generated alright, but it is not visible to your code as a regular field.

Here's your typical automatic property:

public string FirstName  { get; set; }

which, if we look at the compiled assembly, produces this backing store field:

[CompilerGenerated]
private string <FirstName>k__BackingField;

Note the < and > in there, which are not characters you can use in your own field names. Nor can you access that field, because it doesn't "exist" as far as the compiler cares, when it looks at your code.

The real question here, from me, is why you would want to access that field. In other words, why do you need access to the field, and what does it do for your code that accessing the property don't do?

If you want to prevent outside write-access to the field, that's easily doable by making the setter method private, like this:

public string FirstName  { get; private set; }

Note that since the field is actually present in the assembly, it means that this is not JITter magic, but compiler magic, and thus you could use reflection to find, and access, that field.

But again, why would you want to?

Now, let's assume that there really is a legitimate reason for you wanting to use the field, instead of the property. I can think of one, though I would probably do it differently, and that would be that you want to pass the field name to a method as an out or ref parameter, like this:

public void AdjustName(ref String name)
{
    name = Capitalize(name);
}

You can't pass properties as out/ref-parameters, so this code won't work:

AdjustName(ref string FirstName);

In this case, you need to fall back to the "old" way of defining properties, adding the backing store field manually, like this:

private string firstName;
public string FirstName
{
    get { return firstName; }
    set { firstName = value; }
}

With this in place, you can call that method:

AdjustName(ref string firstName); // note the field, not the property

However, I would probably change that method in order to return the new value, instead of directly adjusting a referenced variable.




回答2:


As already said: You cannot access the automatically generated variable (without using bad tricks). But I assume you are asking that question because you want to have only a getter, but still want to use automatic properties ... right? In that case you can use this one:

public string FirstName  { get; private set; }

Now you have a private setter and a public getter.




回答3:


If you use automatic properties, then you cannot access the private field. It is not only private, it is also anonymous.




回答4:


So how can i use /access the private variable,if i want to use it in a method in this class ?

In your source code you only need to, and are only allowed to work with the Property Accessor person.LastName or LastName if you are writing code inside the person class.

The compiler will automatically create a private member variable, or backing field, but this exists in the CIL code. It does not exist anywhere in your source code.



来源:https://stackoverflow.com/questions/1277018/c-sharp-3-0-automatic-properties-what-would-be-the-name-of-private-variable-c

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!