Advertisement
Advanced C# Programming, Access Modifiers in C#

Can the accessibility of a type member be greater than the accessibility of its containing type?

Short answer

No, a member cannot be more accessible than the type that contains it, since that would create a contradiction where the member is technically reachable but the type needed to access it is not visible in the same context.

Save C# Programming mock interview
Advertisement
AModel answer
No, a member cannot be more accessible than the type that contains it, since that would create a contradiction where the member is technically reachable but the type needed to access it is not visible in the same context. For example, if a class is declared as internal, none of its members can be declared as public, because doing so would imply the member is accessible from outside the assembly while the class itself is not.
internal class Helper {
    // This causes a compiler error
    // public void DoWork() { }
    internal void DoWork() { } // valid, matches or is more restrictive than the class
}
Real-world example
A developer marks a class as internal but initially declares one of its methods as public, triggering a compiler warning about inconsistent accessibility, which is resolved by changing the method to internal to match the class's own accessibility level.
Likely follow-ups
  • What is the general rule for matching accessibility between a type and its members?
  • Does this restriction apply to nested classes as well?
Related topics