1

Why is this illegal in C#?

class Foo: Foo.BaseFoo //Circular base class dependency compile time error
{
   private class BaseFoo {...}

   ...
}

I'm not arguing when this could be useful or not, but I'd like to know what are the reasons that would disallow such code to compile. A similar restriction happens with private interfaces.

UPDATE

Seeing that its a duplicate I'll center the question more in why this isn't valid with interfaces which seems more useful?

And, what's more, why does it seem to be legal with the Roslyn preview as shown here

Community
  • 1
  • 1
InBetween
  • 32,319
  • 3
  • 50
  • 90
  • 1
    To compile BaseFoo, Foo has to be compiled first. To compile Foo, BaseFoo has to be compiled first. To compile BaseFoo ... – Fildor Jul 18 '14 at 10:57
  • @Fildor: That's not strictly true. Why does `Foo` *have* to be compiled before `BaseFoo` in the general case? – Jon Jul 18 '14 at 10:58
  • 1
    @Tomtom: No, it will give the same compiler error. – Lasse V. Karlsen Jul 18 '14 at 10:58
  • @Jon That was just a shot into the blue, but that's what I am reading out of the error message. – Fildor Jul 18 '14 at 10:59
  • There are two issues here, one is the class inheriting a nested class, the other is a class inheriting a less visible class. – Lukazoid Jul 18 '14 at 10:59
  • 1
    @Fildor There is no evidence to suggest this is true. The C# compiler is two-pass, it checks type declarations and signatures before moving on to code inside methods. This is probably more an explicit design choice more than a technical limitation. There may be hidden gotchas we can't see, and there may be technical limitations in the compiler, but it does seem arbitrary. – Lasse V. Karlsen Jul 18 '14 at 11:00
  • 2
    @Fildor: After seeing the dupe it seems the issue is as I initially suspected: there is no technical reason this can't be done, but it would introduce a lot of complexity for very very little apparent benefit. – Jon Jul 18 '14 at 11:00
  • @Lukazoid Making both classes public does not change the outcome, and the question was *why* the first is an issue, not *whether* it is an issue. – Lasse V. Karlsen Jul 18 '14 at 11:00
  • @LasseV.Karlsen I am merely stating that even if inheriting from nested classes were supported, this situation would still have an issues with inheriting from a less visible class. But you are correct, the current issue is because of the nested class. – Lukazoid Jul 18 '14 at 11:03
  • @Jon and Lasse : I see. My statement actually just was a quick and dirty interpretation of the error message. Thank you for clarification. +1 for you. – Fildor Jul 18 '14 at 11:03

1 Answers1

1

This is not a problem of the access modifier -that the class is private. This is a circular base class dependency. You try to define a class called Foo that inherits a nested class called BaseFoo.

Christos
  • 53,228
  • 8
  • 76
  • 108