自动生成属性{get;设置;} 与 {获取; C# 中的私有或受保护集;}

发布于 2024-12-06 18:53:35 字数 428 浏览 0 评论 0原文

我看到很多代码使用自动生成的属性,例如 {get;私有集;}{get;受保护集;}

这个privateprotected 集有什么优点?

我尝试了这段代码,但是当我有 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

妄断弥空 2024-12-13 18:53:35

它使外部源(即不是 MyClass 和/或其子类的类)的属性变为只读。或者,如果您使用 private set 声明属性 protected,则该属性对其子类是只读的,但其自身可写。

这对您的类没有影响,因为您的 setter 对于该类来说是私有的,因此您的类仍然可以访问它。但是,如果您尝试从另一个类实例化 MyClass ,并且 Foo 属性具有私有或受保护的 setter,则您将无法修改该属性的值。

privateprotected 在这里的含义与其他地方相同: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 property protected with a private 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 the Foo property's value if it had a private or protected setter.

private and protected mean the same here as they do elsewhere: private restricts access only to that very class, while protected restricts access to that class and all its derived classes.

臻嫒无言 2024-12-13 18:53:35

当您拥有使用继承的类模型时,情况会有所不同。如果您的 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.

兔小萌 2024-12-13 18:53:35

如果您在 getset 关键字上未指定访问修饰符,则将根​​据属性本身的访问修饰符来访问该属性。在您的示例中,如果指定 get 而不是 private get,您将能够从程序中的任何位置获取 Foo 的值并设置 Foo 的值。

为了编写健壮的代码,您应该尝试始终选择尽可能严格的访问修饰符。最好使用属性来公开对象的状态,但不要从外部更改对象的状态。如果您想更改对象的状态,请改用方法调用。

If you specify no access modifiers on the get and set 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 specify get instead of private 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.

客…行舟 2024-12-13 18:53:35

从访问器和修改器方法的角度考虑 getset 函数(除了您不必显式写出方法体:

private int foo;

public int get_Foo()
{
    return foo;
}

public /* or protected, or private */ void set_Foo(int value)
{
    foo = value;
}

在您看到它之后,知道 protectedprivate 修饰符在 setter 上的工作方式与在任何其他类型的成员上的工作方式相同。

Think of the get and set functions in terms of accessor and mutator methods (except that you don't have to explicitly write the method bodies out:

private int foo;

public int get_Foo()
{
    return foo;
}

public /* or protected, or private */ void set_Foo(int value)
{
    foo = value;
}

After you see it like that, know that the protected and private modifiers work the same on a setter as they do on any other sort of member.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文