Different property modifier properties?

I have doubts about whether we can have a different access modifier for get and set in the property.

Thanks,

+4
source share
2 answers

Yes, you can, however, fall under the rule that your getter / setter cannot have a less restricted access modifier than the property itself.

For example (C #):

public Foo { get; private set; } //this is okay protected Bar { get; public set; } //this will throw a compile error 
+8
source

You can restrict the getter or setter properties:

 public string MyProperty { get { return _myProperty; } private set { _myProperty = value; } } 

It also works with internal and secure. However, the key word here is "limited" - you cannot make the modifier more accessible than the general modifier.

+4
source

All Articles