Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

internal interface *less* accessible than an internal protected constructor?

I have an interface and an abstract base class defined in the same assembly:

IFoo.cs:

internal interface IFoo { ... }

Base.cs:

public abstract class Base
{
    internal protected Base(IFoo foo) { ... }
}

This generates the following compiler error:

CS0051: Inconsistent accessibility: parameter type 'IFoo' is less
        accessible than method 'Base.Base(IFoo)'

If I make the Base class constructor internal-only, the error goes away. Since the class is abstract, maybe adding protected to the accessibility doesn't accomplish anything...

Still, I don't understand the error. MSDN defines 'protected internal' as

"Access is limited to the current assembly or types derived from the containing class"

How is the internal interface IFoo less accessible than an internal protected constructor?

like image 975
Scott Smith Avatar asked Jul 25 '13 16:07

Scott Smith


People also ask

What is protected internal?

protected internal: The type or member can be accessed by any code in the assembly in which it's declared, or from within a derived class in another assembly.

What is accessibility modifier protected internal?

The protected internal keyword combination is a member access modifier. A protected internal member is accessible from the current assembly or from types that are derived from the containing class. For a comparison of protected internal with the other access modifiers, see Accessibility Levels.


1 Answers

This MSDN page defined 'protected internal' as (emphasis from original):

The protected internal accessibility level means protected OR internal, not protected AND internal. In other words, a protected internal member can be accessed from any class in the same assembly, including derived classes. To limit accessibility to only derived classes in the same assembly, declare the class itself internal, and declare its members as protected.

So in other words, types from outside the current assembly that derive from Base would have access to Base(IFoo foo) but they wouldn't have access to IFoo, since it is internal. Thus the error.

like image 117
Tim Avatar answered Sep 21 '22 17:09

Tim