C# 私有字段和私有属性之间的差异
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/411048/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Differences between Private Fields and Private Properties
提问by Yoann. B
What is the difference between using Private Properties instead of Private Fields
使用私有属性而不是私有字段有什么区别
private String MyValue { get; set; }
// instead of
private String _myValue;
public void DoSomething()
{
MyValue = "Test";
// Instead of
_myValue = "Test";
}
Is there any performance issue ? or just a naming convention ?
是否有任何性能问题?还是只是一个命名约定?
采纳答案by tvanfosson
Private properties allow you to abstract your internal data so that changes to the internal representation don't need to affect other parts of your implementation, even in the same class. Private fields do not offer this advantage. With automatic properties in C# 3.0, I rarely see a need to implement fields directly -- private or public.
私有属性允许您抽象内部数据,以便对内部表示的更改不需要影响实现的其他部分,即使在同一个类中。私有字段不提供此优势。使用 C# 3.0 中的自动属性,我很少看到需要直接实现字段 - 私有或公共。
回答by Rowland Shaw
Property access will be (fractionally) slower as it will call the getter/setter. The benefit is that you can do data validation, which can then filter down to inheritors if you change the property to be protected, for instance.
属性访问将(部分)更慢,因为它会调用 getter/setter。好处是您可以进行数据验证,例如,如果您更改要保护的属性,则可以过滤到继承者。
回答by Raminder
You would seldom want to make a property private. The provision for a property to be private is provided only for the sake of completeness. And if your property is simply getting/setting the value of the field then there is no performance difference because it will most likely be inlined by the JIT compiler.
您很少希望将财产设为私有。提供私有财产的规定只是为了完整性。如果您的属性只是获取/设置字段的值,则没有性能差异,因为它很可能会被 JIT 编译器内联。
回答by JoshBerke
Other then what has already been answered, Performance, symantics and completness there is one valid case I have seen for private properties instead of a private field:
除了已经回答的内容,性能、语义和完整性之外,我还看到了一个有效的私有属性案例,而不是私有字段:
public class Item
{
private Item _parent;
private List<Item> _children;
public void Add(Item child)
{
if (child._parent != null)
{
throw new Exception("Child already has a parent");
}
_children.Add(child);
child._parent=this;
}
}
Let's say that we don't want to expose Parent for whatever reason, but we might also want to do validation checks. Should a parent be able to be added as a child to one of its children?
假设我们不希望出于任何原因公开 Parent,但我们可能还想进行验证检查。是否可以将父级作为子级添加到其子级之一?
To resolve this you can make this a property and perform a check for circular references.
要解决此问题,您可以将其设为属性并检查循环引用。
回答by JaredPar
The big gain you can get from a property (private, public, ...) is that it can produce a calculated value vs. a set value. For example
您可以从财产(私人、公共……)中获得的最大收益是它可以产生计算值与设定值。例如
class Person {
private DateTime _birthday;
private int _age { get { return (DateTime.Now - _birthday).TotalYears; }
}
The advantage of this pattern is that only one value must be updated for N other values to reflect the change. This is true of properties regardless of accessibility. There is no specific advantage of a private property vs non-private property (other than it being private of course)
这种模式的优点是对于 N 个其他值,只需更新一个值即可反映更改。无论可访问性如何,属性都是如此。私有财产与非私有财产相比没有特别的优势(当然,私有财产除外)
回答by tylermac
When dealing with private access, the differences are very small. Yes, there is a performance hit (which may be optimized by the JIT) send properties represent a method call, instead of a direct address access.
在处理私有访问时,差异非常小。是的,有一个性能损失(可能由 JIT 优化)发送属性表示方法调用,而不是直接地址访问。
The main advantage to using properties is to allow changing the implementation without changing the external signature required. Since these are privately accessed, any changes to the implementation only effects local code.
使用属性的主要优点是允许更改实现而不更改所需的外部签名。由于这些是私人访问的,因此对实现的任何更改只会影响本地代码。
When dealing with private members, I see no advantage gained from properties, except for your team's conventions.
与私人成员打交道时,除了您团队的约定外,我看不到从属性中获得的任何优势。