I’m using C# 4.
The comments explain the purpose of the classes and why they’re structured the way they are.
/// <summary>
/// I want this internal just to reduce the number of publicly
/// accessible classes in this assembly. I don't want people to
/// come after me to even spend time reading about this class,
/// since it won't help them unless they're modifying, and not
/// using this assembly.
/// </summary>
internal class MyInternalClass
{
}
/// <summary>
/// This class is an initialization class for tests. This is the
/// base class for other initialization classes so that I can have
/// a tree-like structure of method calls. This needs to be
/// public because it is the base class of a derived class that needs
/// to be public.
/// </summary>
public class MyPublicClass
{
protected internal MyInternalClass MyInternalProperty;
}
/// <summary>
/// This class is a test class that tests a particular initialization
/// class. This class needst to be public, else the Unit Testing
/// framework will not execute its methods.
/// </summary>
public class MyInheritedPublicClass : MyPublicClass
{
}
Can anyone tell me why I’m getting an inconsistent visibility error for the above code?
As I understand here are the meanings of the visibility modifiers in C#:
public: Everyone, everywhere can see and access this without any sort of reflection hacks.
private: This is only accessible within the scope of the class it’s declared in. Derived classes and other classes in the same namespace and assembly cannot see this.
protected: This is only accessible within the scope of the class it’s declared in and any derived classes, regardless of the assembly or namespace, provided the class is public. If the class is internal then of course that will further limit the classes that can see this property.
internal: This is only accessible to entities within the same assembly.
I thought a protected internal modifier would act like the intersection of protected and internal in a venn-diagram — it would only be accessible to entities that were in the same assembly and were a subclass of said class where the property/method/constructor/field/whatever exists. Given my beliefs, I thought the preceding code should compile.
protected internalis not an intersection ofprotectedandinternal, it is an union, which means thatprotected internalproperty will be visible to all descendants of class irregardless of which assembly they are in.Reference: http://msdn.microsoft.com/en-us/library/ms173121.aspx