What is a private interface?

C#

C# Problem Overview


In an interview a while ago for a .NET position the interviewer asked me "what would you use a private interface for?".

I asked him did he mean the difference between implicit vs explicit interface implementation to which he answered no.

So I'm wondering:

  1. What he meant?
  2. What you would use a private interface for?

C# Solutions


Solution 1 - C#

An interface could be private within another class

public class MyClass
{
	private interface IFoo
	{
		int MyProp { get; }
	}
	
	private class Foo : IFoo
	{
		public int MyProp { get; set; }
	}
	
	public static void Main(string[] args)
    {
		IFoo foo = new Foo();
		return foo.MyProp;
	}
}

in terms of utility it simply hides from other code, even within the same assembly, that said interface exists. The utility of this is not terribly high in my opinion.

Explicit interface implementation is a different matter, has some very useful cases (especially when working with generics and older non generic interfaces) but I would not term it 'private interfaces' and would not say that the term is commonly used in that manner.

Using the two techniques together you can do:

public class MyClass
{
	private interface IFoo
	{
		int MyProp { get; }
	}
	
	public class Foo : IFoo
	{
		int IFoo.MyProp { get; set; }
	}
	
	public static void Main(string[] args)
    {
		IFoo foo = new Foo();
		return foo.MyProp;
	}
}

public class HiddenFromMe
{
	public static void Main(string[] args)
    {
		MyClass.Foo foo = new MyClass.Foo();
		return foo.MyProp; // fails to compile
	}
}

This allows you to expose the nested classes in some fashion while allowing the parent class to invoke methods on them that the outside world cannot. This is a potentially useful case but is not something I would wish to use very often. Certainly it's use in an interview smacks of being a boundary case the interviewer is using because they've seen it and though it was 'interesting'

Solution 2 - C#

From this link.

>Private Interface Inheritance > >Historically, languages have permitted private inheritance. In C++, you can inherit from a type without being polymorphically compatible with that type. It’s just a convenient way to reuse an implementation. In the CTS, you cannot do private implementation inheritance. But you can use private interface inheritance. > >Private interface inheritance is really just a way to hide methods from a type’s public API. They are compiled into private methods but are actually accessible through a type’s interface map. In other words, they can only be called through a reference typed as the interface on which the method is defined. An example will make this easier to understand: > class PrivateImplementer : IFoo { void IFoo.Foo() { Console.WriteLine("PrivateImplementer::IFoo.Foo"); } } In this case, PrivateImplementer is publicly known to implement IFoo. Thus, an instance can be treated polymorphically as an instance of IFoo. But you cannot actually call Foo on it unless you do treat it as an IFoo. This code demonstrates this: > PrivateImplementer p = new PrivateImplementer(); p.Foo(); // This line will fail to compile IFoo f = p; f.Foo();

>You can select individual methods of an interface to implement privately. For instance, if PrivateImplementer implemented IFooBar, it might choose to implement Foo privately, but Bar publicly using the ordinary syntax.

>In practice, there aren’t many common cases where you would use private implementation. The System.Collections.Generic library uses this approach to secretly implement all of the legacy System.Collections weakly typed interfaces. This makes backwards compatibility "just work," for example passing an instance of List<T> to a method that expects an IList will work just fine. In this specific example, cluttering the new type APIs would have been a pity (there are quite a few methods necessary for the weakly typed interoperability).

"No," is a pretty poor answer if he was looking to find out what you knew. Sounds like someone who just wants to show how much they know.

Solution 3 - C#

ShuggyCoUk gives good answer but with such comment

> This is a potentially useful case but is not something I would wish to use very often. Certainly it's use in an interview smacks of being a boundary case the interviewer is using because they've seen it and though it was 'interesting'

I, must to say, it's definitely not ability for only smart interviewers.

Here is Realization of Full State Machine (FSM) with inheritance and unitest support, which is good example of private/protected interfaces usage.

It was an answer on questions What is the C# equivalent of friend? and Why does C# not provide the C++ style 'friend' keyword? and, in fact, on your question too.

Solution 4 - C#

Just like an inner class (which is also private) you can use a private interface in an existing class.

Solution 5 - C#

I googled around a bit and found this article explaining how private interfaces can be used to provide different interfaces to different clients. This is C++ story.

I don't think this can be applied to C# tho, because the same effect IMO can be achieved with explicit interfaces and clients that cast host to appropriate interface.

Maybe somebody else can see something I missed there....

I also found this at MSDN:

> Interface methods have public > accessibility, which cannot be changed > by the implementing type. An internal > interface creates a contract that is > not intended to be implemented outside > the assembly that defines the > interface. A public type that > implements a method of an internal > interface using the virtual modifier > allows the method to be overridden by > a derived type that is outside the > assembly. If a second type in the > defining assembly calls the method and > expects an internal-only contract, > behavior might be compromised when, > instead, the overridden method in the > outside assembly is executed. This > creates a security vulnerability.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionRichardODView Question on Stackoverflow
Solution 1 - C#ShuggyCoUkView Answer on Stackoverflow
Solution 2 - C#Mark DickinsonView Answer on Stackoverflow
Solution 3 - C#max_cnView Answer on Stackoverflow
Solution 4 - C#Gerrie SchenckView Answer on Stackoverflow
Solution 5 - C#majkinetorView Answer on Stackoverflow