When building a C# interface, you may find a need for both public and internal methods, such as:

public class MyClass : IMyInterface
{
    public void MyPublicMethod() { }
    internal void MyInternalMethod() { }
}
public interface IMyInterface
{
    public void MyPublicMethod();
    internal void MyInternalMethod();
}

(For simplicity in this example, we’ll only discuss methods, but this also works for properties, events and indexers.)

Unfortunately, the code above will not compile due to the following errors:

Compiler Error CS0106:  The modifier ‘public’ is not valid for this item
Compiler Error CS0106:  The modifier ‘internal’ is not valid for this item

Access modifiers such as “public” and “internal” are not allowed for interface members.   That’s because the access modifier for the interface itself determines the access level for all members defined in the interface.  Hence, adding an access modifier to an interface member would be redundant.  But that also means that you cannot mix public and internal members in the same interface.

Separate Public and Internal Interfaces

The solution is to create two interfaces, one public and one internal, such as:

public class MyClass :
    IMyPublicInterface, IMyInternalInterface
{
    public void MyPublicMethod() { }
    internal void MyInternalMethod() { }
}
public interface IMyPublicInterface
{
    void MyPublicMethod();
}
internal interface IMyInternalInterface
{
    void MyInternalMethod();
}

Unfortunately, this new code fails to compile due to another error:

Compiler Error CS0737:  ‘InternalInterface.MyClass’ does not implement interface member ‘InternalInterface.IMyInternalInterface.MyInternalMethod()’. ‘InternalInterface.MyClass.MyInternalMethod()’ cannot implement an interface member because it is not public.

A method that implements an interface member must have public accessibility.  So then how do you create an internal interface?

Explicit Interface Members

The trick is to use an explicit interface member implementation.

“An explicit interface member implementation is a method, property, event, or indexer declaration that references a fully qualified interface member name.  Because explicit interface member implementations are not accessible through class or struct instances, they allow interface implementations to be excluded from the public interface of a class or struct.  This is particularly useful when a class or struct implements an internal interface that is of no interest to a consumer of that class or struct.”

So in this example, you would define the internal method in the class with its interface prefix, and remove the internal access modifier, as shown:

void IMyInternalInterface.MyInternalMethod() { }

Internal Interface Example

So here is the working example of an object with both internal and public interfaces:

public class MyClass :
    IMyPublicInterface, IMyInternalInterface
{
    public void MyPublicMethod() { }
    void IMyInternalInterface.MyInternalMethod() { }
}
public interface IMyPublicInterface
{
    void MyPublicMethod();
}
internal interface IMyInternalInterface
{
    void MyInternalMethod();
}

Two Interfaces Means Two References

Don’t forget that you will need one reference for each interface.  This means each object will have two references: a reference to the object’s public interface, and a reference to the object’s internal interface:

MyClass obj = new MyClass();
IMyPublicInterface objPub = obj;
IMyInternalInterface objInt = obj;
objPub.MyPublicMethod();
objInt.MyInternalMethod();

Share and Enjoy:
  • Digg
  • Twitter
  • Facebook
  • Reddit
  • StumbleUpon
  • LinkedIn
  • Google Bookmarks
  • Slashdot