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();
I know it’s not the point of the article but I would take any desire to have both public and internal methods on an interface as a ‘code smell’ that you are violating the Single Responsibility Principle.
@Chris: I respectfully disagree. Having a public and internal interface is no smellier than having both public and internal members in a class, which of course is very common. Note that API clients will see only a single interface. The internal interface gives the DLL authors a backdoor into their own code.
An internal interface is a desirable thing when you want the interface for dependency injection but don’t want public exposure. I’m not certain why odd syntax is required just to get an internal-only interface implemented. I.e, I feel that this should be valid:
======
public class MyClass : IMyInternalInterface
{
internal void MyInternalMethod() { }
}
internal interface IMyInternalInterface
{
void MyInternalMethod();
}
======
There’s no obvious excuse here for the compiler to complain that MyInternalMethod needs to be public, as the interface itself is internal. Yet, it doesn’t work this way; the explicit implementation seems to be needed.
Thanks for this post!
Hello,
I would like to share one of my new experiences related to .NET and VoIP. Recently I have find
a sample WPF softphone the source code of it which was written in .Net 4.0. It is a VoIP softphone and
it can forward and receive calls and DTMF signals. In addition with the help of them the caller party can
navigate in IVR systems easily. It is very necessary in my opinion for the companies which has great number of
customers.
If you are interested in, you can read about this solution and find the source code of the sample software here:
http://www.voip-sip-sdk.com/p_116-how-to-build-a-c-open-source-voip-softphone-with-wpf-gui-using-ozeki-sip-sdk-voip.html
This source code can be developed, and it can be the basis of a customized softphone, that fits to your need.
Good developing!
Hi I would like to share the basic of interface,in order to fully understand how it work.
Please visit http://www.dsourceweb.com/advance-tutorial/interface
if you are interested to learn.
Thanks!
The code in your example is wrong. You say “create two interfaces, one public and one internal”, but then you write an example with two public interfaces and no internal interface. I think you meant to write “internal interface IMyInternalInterface”. If the interface is public, someone referencing your library can still cast an object instance as the public interface and call the “hidden” method.
Another thing: Can’t you have the internal interface implement the public interface so internally you can just cast all references as the internal interface for access to all methods?
@CSharper: Typo fixed, thanks for noticing it. As for your second comment, I’m not sure what you mean, please explain. The point of this article is how to provide a public interface to public methods, and an internal interface to different internal methods.
I think CSharper means like so…
internal interface IMyInternalInterface : IMyPublicInterface
{
void MyInternalMethod();
}
var _obj = (IMyInternalInterface)(new MyClass());
_obj.MyInternalMethod();
_obj.MyPublicMethod();
… now MyClass can be passed around internally as IMyInternalInterface and do everything…and externally it can be passed around like IMyPublicInterface and do only those non-internal things
[…] problem and solution are well explained in C# Internal Interface […]
[…] problem and solution are well explained in C# Internal Interface […]
[…] When required, as per disambiguation or needing an internal interface […]