C# 为什么使用空的 get set 属性而不是使用公共成员变量?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1876197/
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
Why have empty get set properties instead of using a public member variable?
提问by Jeremy
Possible Duplicate:
C#: Public Fields versus Automatic PropertiesDuplicate? I think not:
This question is notthe same as "Why use properties instead of public field". A property with a specified getter and setter is far different than a public field. My question was, is a property WITHOUTa getter and setter, any different.
可能的重复:
C#:公共字段与自动属性复制?我认为不会:
这个问题是不一样的“为什么要使用属性而不是公共领域”。具有指定 getter 和 setter 的属性与公共字段大不相同。我的问题是,没有getter 和 setter的属性有什么不同。
With the somewhat recent ability to have empty getters and setters, what is the benefit of using them instead of just declaring a public member variable?
由于最近有空 getter 和 setter 的能力,使用它们而不是仅仅声明一个公共成员变量有什么好处?
Example:
例子:
public string MyProperty
{
get;
set;
}
versus:
相对:
public string MyProperty;
采纳答案by Webleeuw
One word: inheritance.
一个字:传承。
Properties are inheritable while fields are not. You can use fields in an inherited class, but not alter their behavior by making them virtual.
属性是可继承的,而字段则不是。您可以在继承的类中使用字段,但不能通过使它们成为虚拟来改变它们的行为。
Like so:
像这样:
public class Foo {
public virtual int MyField = 1; // Nope, this can't
public virtual int Bar {get; set; }
}
public class MyDerive : Foo {
public override MyField; // Nope, this can't
public override int Bar {
get {
//do something;
}
set; }
}
Edit: Besides the fact of inheritance, the points pointed out in the other answers (like visibility) are also a huge benefit of properties over fields.
编辑:除了继承的事实外,其他答案中指出的要点(如可见性)也是属性相对于字段的巨大好处。
回答by Ronald Wildenberg
One thing you can do with properties that you can't do with fields is limit visibility for either setter or getter:
您可以使用字段无法使用的属性做的一件事是限制 setter 或 getter 的可见性:
public string MyProperty { get; private set; }
Something I use quite a lot.
我经常使用的东西。
And something (more powerful) you can't do with fields is define them inside an interface. Suppose you want an interface that requires implementing classes to have a certain property:
你不能用字段做的事情(更强大)是在接口中定义它们。假设您想要一个需要实现类具有特定属性的接口:
public interface MyInterface
{
string MyProperty { get; }
}
Note that you do not need to have a setter here. It is entirely up to implementing classes to determine how they should set MyProperty
.
请注意,这里不需要设置器。完全取决于实现类来确定它们应该如何设置MyProperty
。
回答by Lucero
Fields cannot be exposed in interfaces. And the auto-property can be changed into a "normal" property at any time if needed, without having the signature and interface of the class changing.
字段不能在接口中公开。如果需要,自动属性可以随时更改为“正常”属性,而无需更改类的签名和接口。
In general, fields are considered to be an implementation detail, which may change in future versions of the code. Therefore, you should expose data via methods and properties, leaving the way open for internal changes in the future which do not affect code using the class.
一般来说,字段被认为是一个实现细节,在未来的代码版本中可能会发生变化。因此,您应该通过方法和属性公开数据,为将来不影响使用该类的代码的内部更改留出余地。
回答by Amit
The idea is to manage the values inside of the object, state, avoiding corruption and misuse by calling code.
这个想法是通过调用代码来管理对象内部的值、状态,避免损坏和误用。
回答by M4N
A property gives you several advantages over a simple public field:
与简单的公共字段相比,属性为您提供了几个优势:
- you can control whether the property is read-only, write-only, or read/write
- you can hide the actual implementation (maybe in the setter you want to do more than just setting a value)
- when using databinding (e.g. in ASP.NET), you'll have to use properties (does not work with fields)
- 您可以控制属性是只读、只写还是读/写
- 你可以隐藏实际的实现(也许在 setter 中你想做的不仅仅是设置一个值)
- 使用数据绑定时(例如在 ASP.NET 中),您必须使用属性(不适用于字段)
回答by Chris Clark
You can flag properties with attributes that aren't available on members. There are attributes that only apply to fields in the DataContract namespace that affects serialization, the attribute can't be applied to fields, etc.
您可以使用对成员不可用的特性来标记属性。有些属性仅适用于影响序列化的 DataContract 命名空间中的字段,该属性不能应用于字段等。
Admittedly, there isn't anything technically preventing these attributes from being used on members, but nonetheless they only work on properties.
诚然,从技术上讲,没有任何东西可以阻止这些属性用于成员,但尽管如此,它们仅适用于属性。
回答by Wil P
Tight coupling comes to mind. Using public fields removes the layer of abstraction made available by the use of properties. Using private fields and properties hides the implementation from other classes and helps to insulate them (external classes) whenever a change is necessary.
紧耦合浮现在脑海中。使用公共字段消除了通过使用属性提供的抽象层。使用私有字段和属性可以隐藏其他类的实现,并有助于在需要更改时隔离它们(外部类)。
Also, keep in mind that you are referring to auto-implemented propertieswhich causes the compiler to create the backing field for you instead of you having to manually create the backing (private) field for each property on your class.
另外,请记住,您指的是自动实现的属性,它会导致编译器为您创建支持字段,而不必为类中的每个属性手动创建支持(私有)字段。