道具和完整财产有什么区别?
下面两段代码有什么区别吗?或者顶部只是底部的一个简短形式?
public string Name { get; set; }
和
private string _Name;
public string Name
{
get { return _Name; }
set { _Name = value; }
}
Is there any difference between the two pieces of code below? Or is the top just a short form of the bottom one?
public string Name { get; set; }
and
private string _Name;
public string Name
{
get { return _Name; }
set { _Name = value; }
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(7)
唯一的区别(除了您必须在类构造函数中使用“默认名称”进行初始化之外)是
_Name
在类本身中可见。存在该类将在内部引用_Name
而不是Name
的风险,一切都会正常工作,并且在稍后的某个时间点,您将向添加一些逻辑由于您在类中使用
。_Name
,因此不会调用 Name例子:
The only difference (other than the fact you would have to do the initialization with "Default Name" in your class constructor) is that
_Name
will be visible within the class itself. There's a risk that the class will internally reference_Name
rather thanName
, everything will work fine, and at some later point in time you'll add some logic toName
that will not be called because you're using_Name
within the class.Example:
这两个属性方法的基本行为和目的几乎相同。但主要的区别在于实施。 之间的区别
AND
是,如果您使用短属性语法(在框架 3.0 或更高版本中引入),则属性字符串永远不会初始化,即如果您在任何地方直接使用“Name”属性而不为其设置值,它将返回 NULL 值。但是,如果您使用第二种语法来初始化属性值,它将返回一个 EMPTY 字符串,因为当您初始化一个字符串时,它是用 EMPTY 值而不是 NULL 初始化的。因此,如果您在没有使用 FULL 属性方法初始化的情况下返回属性值,它将始终返回 EMPTY 字符串而不是 NULL 值。
Basic behavior and purpose of both of the property method is almost same. But the major difference is in the implementation. The difference between
AND
is if you use short property syntax (introduced in framework 3.0 or later), then the property sting is never initialized i.e. if you directly use "Name" property anywhere without setting the value to it, it will return a NULL value. But if you use second syntax to initialize the property value, it will return a EMPTY string because when you initialize a string, it is initialized with a EMPTY value not the NULL. So if you return the property value without initializing using FULL Property Method, it will always return the EMPTY string not the NULL value.
我不认为编译后的代码有任何区别。您可能想要执行完整操作的原因是,如果您想添加默认值(可以在构造函数中以简写形式完成),或者向 getter 或 setter
编辑添加额外的代码:实际上,您的代码错了,应该不是
……
I dont think there is any difference in compiled code. The reason why you may want to do the full way though is if you want to add a default value (which can be done in the constructor in short hand form), or add additional code to the getter or setter
EDIT: Actually, your code is wrong it should be
not...
一个区别是,执行此操作时,您可以在私有字符串上设置默认值。
编译后,您显示的两个示例是相同的。
One difference is that you can set a default on the private string when you do this
Once compiled the two examples you showed are the same.
它只是一个简短的形式,底层变量仍然作为支持支持字段(存储数据的位置)生成,但是自动生成 - 如果您实际上只是获取和设置并且不需要任何特定的实现细节,那么这非常有用任一访问器。
It is simply a short form, the underlying variable is still generated as a supporting backing field (where the data is stored,) but automatically - this is useful if you are literally just getting and setting and don't need any specific implementation details in either accessor.
对于第二种形式的特定实现,两者是等效的。因为如果你只写第一种形式,编译器会生成几乎相同的代码。
也就是说,编译器将向其中添加代码:
使其看起来像这样:
顺便说一句,这是不正确的,
我认为您的意思是:
For this particular implementation of second form, both are equivalent. Because the compiler will generate almost the same code if you simply write the first form.
That is, the compiler is going to add code to it:
making it look like this:
By the way, this is incorrect
I think you meant:
对于所写的示例,它们是完全相同的。
自动实现的属性是为了准确解决此类情况而引入的语法糖,其中使用属性只是为了避免出现公共字段,而 getter/setter 中没有额外的逻辑。但是,自动实现的属性为您提供了属性的所有优点,包括元数据。这是一个相当旧但仍然相关的链接,对它们进行了更多解释。
在幕后,编译器会生成一个与您自己的非常相似的支持字段。
For the example as written they are an exact equivalent.
Auto-implemented properties are syntactic sugar introduced to address exactly these type of situation, where the property is used just to avoid having a public field, with no extra logic in the getter/setter. However, an auto-implemented property gives you all the benefits of properties, including metadata. Here's a rather old but still relevant link that explains a little bit more about them.
Behind the scenes, the compiler generates a backing field very similar to your own.