自动生成属性{get;设置;} 与 {获取; C# 中的私有或受保护集;}
我看到很多代码使用自动生成的属性,例如 {get;私有集;}
或 {get;受保护集;}
。
这个private
或protected
集有什么优点?
我尝试了这段代码,但是当我有 Foo{get; 时,情况是一样的。设置;}
。
public class MyClass
{
public int Foo {get; private set;}
public static void RunSnippet()
{
var x = new MyClass();
x.Foo = 30;
Console.WriteLine(x.Foo);
}
...
}
I see a lot of code uses automatically generated property like {get; private set;}
or {get; protected set;}
.
What's the advantage of this private
or protected
set?
I tried this code, but it's the same when I have Foo{get; set;}
.
public class MyClass
{
public int Foo {get; private set;}
public static void RunSnippet()
{
var x = new MyClass();
x.Foo = 30;
Console.WriteLine(x.Foo);
}
...
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
它使外部源(即不是
MyClass
和/或其子类的类)的属性变为只读。或者,如果您使用private set
声明属性protected
,则该属性对其子类是只读的,但其自身可写。这对您的类没有影响,因为您的 setter 对于该类来说是私有的,因此您的类仍然可以访问它。但是,如果您尝试从另一个类实例化
MyClass
,并且Foo
属性具有私有或受保护的 setter,则您将无法修改该属性的值。private
和protected
在这里的含义与其他地方相同:private
仅限制对该类的访问,而protected
> 限制对该类及其所有派生类的访问。It makes a property read-only by external sources (i.e. classes that aren't
MyClass
and/or its subclasses). Or if you declared the propertyprotected
with aprivate set
, it's read-only by its subclasses but writable by itself.It doesn't make a difference in your class because your setter is private to that class, so your class can still access it. However if you tried to instantiate
MyClass
from another class, you wouldn't be able to modify theFoo
property's value if it had a private or protected setter.private
andprotected
mean the same here as they do elsewhere:private
restricts access only to that very class, whileprotected
restricts access to that class and all its derived classes.当您拥有使用继承的类模型时,情况会有所不同。如果您的 MyClass 方法是您的私有字段和方法的客户端,则没有区别。
也就是说,即使您预计 MyClass 不会成为任何类型的类层次结构中的父类,将字段和方法范围限制为其所需的最不可见范围也没有什么坏处。默认情况下,将您能封装的内容封装在最不可见的范围内,这样当子类开始访问它们不应该访问的父属性时,您就不必进行重构。努力的程度与不努力没有什么不同。
It makes a difference when you have a class model that uses inheritance. If your MyClass methods are clients of your private fields and methods it makes no difference.
That said, even if you don't anticipate your MyClass becoming a parent class in any sort of class hierarchy, it doesn't hurt to limit your field and method scope to the least visible scope that it requires. Encapsulate what you can with the least visible scope by default so that you don't have to refactor when subclasses start to access parent properties that they shouldn't be. The level of effort isn't any different from not doing so.
如果您在
get
和set
关键字上未指定访问修饰符,则将根据属性本身的访问修饰符来访问该属性。在您的示例中,如果指定get
而不是private get
,您将能够从程序中的任何位置获取 Foo 的值并设置 Foo 的值。为了编写健壮的代码,您应该尝试始终选择尽可能严格的访问修饰符。最好使用属性来公开对象的状态,但不要从外部更改对象的状态。如果您想更改对象的状态,请改用方法调用。
If you specify no access modifiers on the
get
andset
keywords, the property will be accessible according to the access modifier of the property itself. In your example, you would be able to get the value of Foo and set the value of Foo from anywhere in your program if you specifyget
instead ofprivate get
.In order to write robust code, you should try to always choose the most restrictive access modifier possible. It is a good idea to use properties to expose the state of your object, but not to change the state of your object from outside. If you want to change the state of your object, use method calls instead.
从访问器和修改器方法的角度考虑
get
和set
函数(除了您不必显式写出方法体:在您看到它之后,知道
protected
和private
修饰符在 setter 上的工作方式与在任何其他类型的成员上的工作方式相同。Think of the
get
andset
functions in terms of accessor and mutator methods (except that you don't have to explicitly write the method bodies out:After you see it like that, know that the
protected
andprivate
modifiers work the same on a setter as they do on any other sort of member.