Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ERROR: the accessibility modifier of the set accessor must be more restrictive than the property or indexer

Tags:

c#

i'm having a bit of confusion with property accessors.

I'd like to have an internal property with its set accessor only accessible to derived classes.

something like this

internal [internalClass] MyProperty
{
get {return _prop;}
protected set {_prop = value;}
}

when i do this the compiler complains.

MSDN, when discussing this particular error suggests changing the set access modifier to private

which is not where i want this to go.

it looks like Protected Internal should be an option here however using this modifier gives the same error

I have a feeling i'm missing some basic understanding of access modifiers.

thanks

like image 855
Beta033 Avatar asked Jul 20 '10 17:07

Beta033


1 Answers

Neither protected nor protected internal is more restrictive than internal. Both would let derived types from a different assembly access the setter but not the getter. protected internal gives access to the union of protected and internal, not the intersection. (There is an access level representing the intersection in the CLR, but it's not exposed by C#.)

You might be best off using a private setter and a protected SetMyProperty method which just calls the private setter, if that matches what you want to achieve.

like image 164
Jon Skeet Avatar answered Sep 23 '22 04:09

Jon Skeet