#797 – Setting Accessibility for Property Accessors
March 11, 2013 Leave a comment
By default, the accessibility of a property accessor matches the accessibility of the property itself. You can also explicitly set the accessibility of an accessor, within certain limitations.
When setting the accessibility of a property accessor:
- You can’t make an accessor more accessible than the property itself
- You can’t specify the exact same accessibility on the accessor as the property
- The property must have both get and set accessors defined
- You can only set accessibility on one of the accessors, not both
- You can’t set the accessibility of an accessor on a property in an interface
- When overriding a virtual property in a parent class, you must replicate the accessibility of the accessors in the parent’s class exactly
The bottom line–you can make one of the accessors a bit more restrictive than the other.
public string Name { get; protected set; }