Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>An interface could be private within another class</p> <pre><code>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; } } </code></pre> <p>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.</p> <p><a href="http://msdn.microsoft.com/en-us/library/aa288461(VS.71).aspx" rel="noreferrer">Explicit interface implementation</a> 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.</p> <p>Using the two techniques together you can do:</p> <pre><code>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 } } </code></pre> <p>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'</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload