C#,带变量和不带变量的属性之间的区别
我知道属性的基本功能。但当我深入查看文档时,我发现它们只是使用 get set 进行声明,而没有使用变量。
这两者之间有什么区别
public int EmpCode
{
get { return _strEmpCode; }
set { _strEmpCode = value; }
}
?
public int EmpCode
{
get;
set;
}
这是否只是随着 .net 框架升级而得到的一种更简单的编写方式。或者有什么功能上的区别吗?
Possible Duplicate:
What's the difference between encapsulating a private member as a property and defining a property without a private member?
I know the basic functionality of properties . But as i go through documentation in depth i see they are declared just with get set and without a variables .
what is the diffeence between these two
public int EmpCode
{
get { return _strEmpCode; }
set { _strEmpCode = value; }
}
and
public int EmpCode
{
get;
set;
}
Is it just a easier way of writing which got as .net frameworks got upgraded . Or is there any functional difference ?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
后者称为自动属性,并且是相同的。它们是在 C#3 中引入的,您可以在这里阅读有关它们的更多信息:http://trashvin.blogspot.com/2008/05/automatic-properties-and-object.html
简单地说,自动属性是语法糖,因此开发人员只需键入更少的代码,编译器将为您生成私有字段以及公共 setter 和 getter。
The later is called an Automatic Property and is the same.They were introduced in C#3, you can read more about them here: http://trashvin.blogspot.com/2008/05/automatic-properties-and-object.html
Simply put, Automatic Properties are syntactic sugar so the developer has to type less code and the compiler will generate the private field and the public setter and getter for you.
这称为自动属性。没有功能上的区别。后一种语法只是前一种语法的简写。
C# 规范 的第 10.7.3 节提供了更多详细信息:
This is called an automatic property. There is no functional difference. The latter syntax is just a shorthand of the former.
Section 10.7.3 of the C# specification gives more detail:
它称为自动属性,如果您不需要属性内部有任何逻辑,那么这是一种更简单的编写方法。编译时,编译器将自动为该属性生成一个支持变量,因此它是完全相同的。
稍后可以很容易地将属性更改为带有支持字段和一些逻辑的属性,并且这将不会破坏依赖于该属性的任何代码,如果您只是先使用公共字段,然后将其更改为属性,您将破坏依赖于该字段/属性的代码。
It's called Auto-properties and is just a easier way of writing them if you don't need any logic inside the property. When compiled, the compiler will auto-generate a backing variable for the property so it's exactly the same.
It's easy to change the property into a property with a backing field and some logic later on, and that will not break any code dependant on this property, If you instead just used a public field first, and then changed it into a property you would break the code that relied on this field/property.
第二种方法实际上是自动属性,它隐式实现支持字段,这意味着您不能影响生成的字段名称。
大多数时候你并不关心,但是在使用序列化/反序列化在层之间传递对象的情况下,在某些情况下你会显式创建支持字段,以便摆脱 __propBackingField2735t34 像客户端上的名称。
此外,在显式编码的属性中,可以包含一些验证逻辑,但自动属性则不然
Second way is actually auto-property which implicitly implement backing field, which means that you cannot affect generated field name.
Most of the time you don't care, however in scenarios when you pass objects between layers using serializing / deserializing , you in some cases would have explicitly created backing field, in order to get rid of
__propBackingField2735t34
like names on the client.Also in explicitly coded properties some logic for validation can be included which is not the case for autoproperites
这是 C# 3.0 的增强功能,称为自动实现属性,编译器在后台自动创建私有支持字段
This is a enhancement from C# 3.0 which is termed as auto implemented properties, a private backing field is automatically created by the compiler in the background
本身没有功能差异。但是如果您想要在属性设置/获取上获得更多功能,您可以使用带有私有变量的版本。
否则您可以简单地使用不带有私有变量的版本。
there is no functional difference as such.. but if you want more functionality on your property setting/getting you may use the version with the private variables..
Otherwise you can simply use the version without the private variables.