2

Given:

namespace Program {

    // Cannot inherit from inner class
    public class Outer : Outer.Inner {
        public class Inner { }
    }

    class Program {
        public static void Main() {
            Outer.Inner inner = new Outer();
        }
    }
}

Why can't outer classes implement inner classes? I could see why this would be illegal in Java because inner classes are actually dependent on the outer instance existing before an inner class can be instantiated. But in C#, inner classes don't have any real dependency on the outer class other than scope (and the rules that come along with it). So why is this illegal? Is it just by convention, or is there some logic to this?

Dmitry Ginzburg
  • 7,391
  • 2
  • 37
  • 48
sircodesalot
  • 11,231
  • 8
  • 50
  • 83
  • Ahah, Jon Skeet to the rescue! – sircodesalot May 15 '14 at 13:59
  • Jon's answer in the duplicate question is of course correct. He refers to some email I sent him on the subject but doesn't go into the details. Suffice to say, deducing what the meaning of the base class clause is in C# is suprisingly difficult and actually ill-founded; thanks to generics you can get into situations where the base class resolver goes into weird loops, and sometimes it detects cycles when it shouldn't, or fails to detect cycles when it should. – Eric Lippert May 15 '14 at 15:39
  • Oh, do you have an example of this (weird loops, and detection errors)? – sircodesalot May 15 '14 at 15:41

0 Answers0