获取私有字符串属性{get; set;}
与私有字符串字段
。
请注意,两者都是私有的(因此它们不会暴露在此类之外),并且该属性未采用额外的验证。
就语义而言,它们是否具有不同的含义?从某种意义上说,这样使用时它们可以互换吗?
当涉及到影响时,例如(微观?)性能,创建字段与属性(即让编译器为您处理支持字段)是否重要。
Take private string Property {get; set;}
versus private string field
.
Note that both are private (so they will not be exposed outside of this class) and that the property is not employing extra validation.
As regards semantics, do they have different meanings? In the sense that, are they interchangeable when used like this?
And when it comes to implications, such as (micro?) performance, does it matter if you create a field versus a property i.e. letting the compiler take care of the backing field for you.
发布评论
评论(5)
当它们是私有的时,我知道的唯一区别是该属性不适合
out
和ref
参数。但大多数情况下,私有财产不会带来任何优势(相对于领域),所以为什么要麻烦呢?
可能存在(微观)性能成本。我会更担心额外的混乱。
When they're private the only difference I know is that the property is not suitable for
out
andref
parameters.But mostly a private property does not deliver any advantages (over a field), so why bother?
There probably are (micro) performance costs. I would worry more about the extra clutter.
更新
性能影响:
使用属性(自动或支持字段)与字段相比,性能稍有下降,因为属性是一种方法,并且需要调用 CLR virtcall 。
但正如我所说,使用属性并没有多大意义,我相信字段更具可读性,因为通常通过命名约定立即可见(以下划线开头 或骆驼肠衣)。
UPDATE
Performance implications:
There is a slight performance in using property (auto or with backing field) vs field since a property is a method and a CLR
virtcall
needs to be called.But as I said, there is not much point in using property and I believe a field is more readable as usually immediately visible by the naming convention (starting with underscore or camel casing).
您无法获得对属性的引用,但可以获得对成员的引用。因此,如果您使用成员,以后无论出于何种原因(例如添加臭名昭著的验证),您可能会在将它们切换为属性时遇到麻烦。
You can't have a reference to a property, but you can get a reference to a member. Thus, if you use members, you might have trouble switching them to properties for whatever reason later, such as adding the notorious validation.
我认为创建私有自动属性没有任何用处。如果它不是自动的,它可以用作某种内部“事件处理程序”以保持对象状态最新:每次代码中任何位置的字段更改(通过设置器)时执行一些操作。
表现?我认为不会有问题,即使是在微观层面上也是如此。
Creating a private automatic property has no use that I can see. If its not automatic it could be used as some kind of internal 'event handler' to keep the object state up to date: perform some actions every time the field changes (through the setter) anywhere in the code.
Performance? I dont think there would be an issue, not even at a micro micro level.
属性是函数。
字段是“至少具有类可见性的变量”。
因此,如果您有私有属性与私有字段:与
性能点的区别:
如果您使用优化且没有跟踪(属性被视为内联),则没有区别。
语义上的差异:
1)形式上没有区别。
2)更深入地说,存在差异。由于属性是函数,因此您可以从 getter 和 setter 获取委托。并且可以使用委托作为...委托,就像将此委托与其他委托一起放入列表中(从属性 getter 或 setter 方法创建委托)
与设计视图的区别:
但属性是看起来像变量的函数。为什么需要看起来像变量的函数?
假设您有 Hand 类,并且这只手有变量 FingerNumber。
然后你可能有很多类似的代码
,但在某些时候你可能想向 Hand 添加一些其他变量。假设它是ringsNumber。而且您知道,每个手指不能有超过 10 个戒指。
现在,您不能这样做,
因为您必须根据 FingerNumber 依赖项来控制ringsNumber。
因此,您必须创建一些函数来检查这种依赖性。此外,您还必须对用户隐藏 FingerNumber 和 RingNumber,以便他们在未经检查的情况下无法更改此字段。
并使用这个函数作为
这里的问题,旧代码 leftHand.fingersNumber-- 现在不起作用。从一开始你就不知道将来会添加戒指。为了解决这些问题,将字段设置为私有并使用 Set 和 Get 函数来获取和更改变量成为一种范例,并确保将来可以在其中添加任何逻辑并且代码可以工作!
Setter 和 Getters 是 C++、Java 和许多语言中的现状。
但 C# 创建者更进一步,将此类 getter 和 setter 函数装饰为“属性”。
但大多数时候人们创建这样简单的属性,并且您看到这个示例,它只有 5 行例行代码。因此,在 C# 的某些版本中添加了自动属性来简化程序员的工作。所以你的类可能看起来像,
但在任何时候你都可以扩展这个 get set 行为:
并且它不会破坏任何代码。 这就是
属性是什么、为什么使用它们以及与字段有什么区别。
正如我之前所说,如果使用优化,简单属性和自动属性与变量具有相同的性能。 Se 反汇编或者只是谷歌一下。
Properties ARE functions.
Fields are 'variables with at least class visibility'.
So if you have private property vs private field:
The difference from the performance point:
no difference if you use optimization and no trace (properties are treated as inlines).
The difference in semantics:
1) Formally no difference.
2) More deeply, there IS a difference. Since properties are functions you CAN get a delegate from getter and setter. And CAN use the delegate as... delegate, like putting this delegate to the list with other delegates (Create a delegate from a property getter or setter method)
The difference from a design view:
But properties are functions that looks like variables. Why one could need functions that look like variables?
Lets say you have class Hand and this hand has variable fingersNumber.
Then you may have a lot of code like
But at some point you may want to add some other variable to Hand. Lets say it is ringsNumber. And you know, that you can't have more than 10 rings for each finger.
now, you cannot just do
because you have to control ringsNumber on fingersNumber dependence.
So you have to create some functions that whould check this dependance. Also you have to hide fingersNumber ander ringsNumber from users so they cannot change this fields without the check.
And use this functions as
The problem here that the old code leftHand.fingersNumber-- would not work now. And from the beginning you wouldn't know that one will add rings in the future. To solve the problems it became a paradigm to set fields as private and use Set and Get functions to get and change variables and BE SURE that in the future one could add any logic there and code would work!
Setters and Getters is a current situation in C++, Java and many languages.
But C# creators went further and decorated such getters and setters functions as "properties".
But most of the time people create such simple property and, you see the example, it is 5 lines of routine code. So at some version of C# autoproperties was added to simplify life of programmers. So your class is probably looks like
but at any time you can extend this get set behaviour:
And it will NOT BRAKE ANY CODE. Like
So thats what are the properties, why do they used and what is the difference with fields.
Also as I ser earlier, simple properties and autoproperties have the same performance as variables if you use optimization. Se disassembly or just google about it.