开发者

Automatically generated property {get; set;} vs {get; private or protected set;} in C#

开发者 https://www.devze.com 2023-04-06 22:09 出处:网络
I see a lot of code uses automatically generated property like {get; private set;} or {get; protected set;}.

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);
    }
...
}


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.


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.


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.


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号