Access modifiers by properties; why doesn't the next job work?

I encountered a compiler error, which for me does not make sense. I have an internal property, and I want to limit its set block to such that it is available only through inheritance. I thought this would work:

 internal bool MyProperty { get { return someValue; } protected internal set { someValue = value; } } 

But the compiler says that the access modifier in the set block should be more strict than internal - did I miss something, or is protected internal not more restrictive than internal ?

+6
access-modifiers c # properties
source share
4 answers

protected internal less restrictive; it is protected either internally (not and )), which, in addition, allows you to access subclasses from other assemblies. You will need to invert:

 protected internal bool MyProperty { get { return someValue; } internal set { someValue = value; } } 

This will allow you to use the code in your assembly, as well as subclasses from other assemblies, to get it (read), but only the code in your assembly can install it (write).

+9
source share

From the documentation on access modifiers in C #:

Protected internal accessibility level means protected OR internal, not protected and internal. In other words, a protected internal member can access from any class in the same collection, including derived classes. To restrict access to only derived classes in the same assembly, declare the class itself is internal and declare its members protected.


To achieve the desired effect, instead you need to change the access modifiers, for example:

 protected internal bool MyProperty { get { return someValue; } internal set { someValue = value; } } 
+3
source share

No, this is the union of the two, not the intersection; therefore, protected internal is less restrictive than both of these individuals. Intersection is not a feature of C #; The CLR supports Family and Assembly, but C # only supports Family OR Assembly.

+2
source share

Here, protected internal less restrictive than internal .

  • protected internal - public for the current assembly and any type that inherits this type in other assemblies.

  • internal - public for this assembly and closed to other assemblies

+1
source share

All Articles