凡/什么是自动实现的属性的私有变量? [英] Where/what is the private variable in auto-implemented property?
问题描述
有没有(明显)引用该名字应该是躲在将firstname私有变量。你能解释一下这是如何工作?
我假设有正在被getted并设置好的一些私有变量。
谢谢
//自动实现的属性名字
公共字符串名字{获得;组; }
基本上编译器代码转换为这样的事情
私人字符串< __>的firstName;
公共字符串名字
{
获得{返回<&__ GT;的firstName; }
组{< __>的firstName =价值; }
}
这是不太可能的确切名称,但在使用尖括号名字是很重要的 - 因为它使得一个的难言的名称的。 (这是非官方的术语,但广泛应用 - 我不知道埃里克利珀是否真的创造,或者他是否只是第一人的东西我读使用它)这是不是一个合法的C#标识符的名字,但在CLR是相当满意的。这有两个好处:
- 编译器并不需要担心使用的您的标识符命名冲突
- 编译器并不需要担心你是否尝试引用外地在自己的代码 - 你不能,因为名字是难言 $ b! $ b
它使用其他各种生成的代码相同的技术 - 匿名类型,匿名函数,迭代器块等。
There is no (explicit) reference to a firstName private variable which FirstName is supposed to be hiding. Could you explain how this works? I assume there is some private variable that is being getted and setted. Thanks.
// auto-implemented property FirstName
public string FirstName { get; set; }
Basically the compiler converts your code into something like this:
private string <__>firstName;
public string FirstName
{
get { return <__>firstName; }
set { <__>firstName = value; }
}
That's unlikely to be the exact name, but the use of angle brackets in the name is important - because it makes it an unspeakable name. (That's unofficial terminology, but widely used - I don't know whether Eric Lippert actually coined it, or whether he was just the first person to use it in something I read.) It's a name which isn't a valid C# identifier, but which the CLR is quite happy with. That has two benefits:
- The compiler doesn't need to worry about naming collisions with your identifiers
- The compiler doesn't need to worry about whether you're trying to refer to the field in your own code - you can't, because the name is unspeakable!
It uses the same technique for all kinds of other generated code - anonymous types, anonymous functions, iterator blocks etc.
这篇关于凡/什么是自动实现的属性的私有变量?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!