C#3.0:自动属性-编译器创建的私有变量的名称是什么 [英] C# 3.0 :Automatic Properties - what would be the name of private variable created by compiler

查看:144
本文介绍了C#3.0:自动属性-编译器创建的私有变量的名称是什么的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在检查.NET 3.5的新功能,发现在C#3.0中,我们可以使用

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; }
}

代替

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 ?

推荐答案

重写后更加清晰

该字段已生成,但是作为常规字段在您的代码中不可见.

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?

如果要防止外部对该字段的写访问,可以通过将setter方法设为私有来轻松实现,例如:

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; }

请注意,由于该字段实际上存在于程序集中,因此这不是JITter魔术,而是编译器魔术,因此您可以使用反射来查找和访问该字段.

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?

现在,让我们假设您确实有合理的理由要使用该字段而不是该属性.我可以想到一个,尽管我可能会做不同的事情,那就是您要将字段名作为out或ref参数传递给方法,例如:

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);
}

您不能将属性作为out/ref-parameters传递,因此此代码不起作用:

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.

这篇关于C#3.0:自动属性-编译器创建的私有变量的名称是什么的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆