Why can't an interface contain types?
<p>This has caught me out once or twice in C#. I can write code such as this</p>

<pre><code>class Node
{
    class Connection
    {
        public Connection(Node node, string label)
        {
            this.Node = node;
            this.Label = label;
        }
        public Node Node { get; private set;  }
        public string Label { get; private set; }            
    };

    IEnumerable&lt;Connection&gt; IncomingConnections() // ...
    IEnumerable&lt;Connection&gt; OutgoingConnections() // ...
}
</code></pre>

<p>but if I write</p>

<pre><code>interface INode
{
    class Connection
    {
        public Connection(INode node, string label)
        {
            this.Node = node;
            this.Label = label;
        }
        public INode Node { get; private set; }
        public string Label { get; private set; }
    };

    IEnumerable&lt;Connection&gt; IncomingConnections();
    IEnumerable&lt;Connection&gt; OutgoingConnections();
}
</code></pre>

<p>I get the compile error</p>

<blockquote>
  <p>error CS0524: 'Connection': interfaces cannot declare types</p>
</blockquote>

<p>I understand the restriction, but what I'm interested in is <strong><em>why</em></strong>. I can certainly have nested types in a C++ "interface" (which is just a class with abstract members so no surprise), and apparently it's possible in Java too, see <a href="http://stackoverflow.com/questions/8051324/interfaces-cannot-declare-type-issue-c-sharp">Interfaces Cannot Declare Type Issue C#</a>. So given that C# learnt some things from Java, why is it lacking in this respect (if indeed it is lacking)?</p>

<p>(Apologies if this has already been addressed elsewhere. I also found <a href="http://stackoverflow.com/questions/15009073/interfaces-cannot-declare-types">Interfaces cannot declare types</a> and <a href="http://stackoverflow.com/questions/612957/why-cant-i-put-a-delegate-in-an-interface">Why can&#39;t I put a delegate in an interface?</a> but they didn't seem to address my question directly.)</p>

<p><em>Edit</em></p>

<p>I thought I'd just add a note to say that in the Java world it seems at first sight to be an open question as to whether it's ok to nest a class within an interface. See <a href="http://stackoverflow.com/a/9098321/834521">http://stackoverflow.com/a/9098321/834521</a>. I don't think I'm being silly in asking why the same can't apply to C#.</p>

<p><em>Edit</em></p>

<p>Brief summary / quotes from <a href="http://rads.stackoverflow.com/amzn/click/0321545613" rel="nofollow">Framework Design Guidelines</a>, 2ed, section 4.9 pp115-117.</p>

<ul>
<li><strong>Do use</strong> nested types when e.g. the nested type needs access to private members of the enclosing type.</li>
<li><strong>Don't use</strong> public nested type for grouping; use namespaces for this.</li>
<li><strong>Avoid</strong> publicly nested types unless you really know what you're doing. (Main motivation: explicitly creating nested types is confusing for less skilled developers. However implicitly creating, e.g. via collection enumerators, is ok.)</li>
<li><strong>Don't use</strong> nested types if the nested type is it is going to be used or instantiated outside of the containing type (both of these argue for independence of the nested type from the containing type).</li>
<li><strong>Don't</strong> use as a member of an interface.</li>
</ul>
