This example compiles:
public interface IOuter : IInner {
}
public interface IInner {
}
And so does this:
public class Outer : Outer.IInner {
public interface IInner { }
}
But when Outer
is an interface, we get a compilation error:
public interface IOuter : IOuter.IInner {
public interface IInner { }
}
IOuter.cs(3, 18): [CS0529] Inherited interface 'IOuter.IInner' causes a cycle in the interface hierarchy of 'IOuter'
Why does inheriting a nested interface cause a cycle?
Does IInner
somehow implicitly inherit IOuter
, preventing IOuter
from implementing IInner
? It doesn't seem like it, because this compiles:
public interface IOuter {
string OuterProperty { get; }
public interface IInner {
string InnerProperty { get; }
}
}
public class InnerImplementation : IOuter.IInner {
// We only need to implement IInner's members, not IOuter's,
// so IOuter does not seem to be part of the dependency hierarchy here
public string InnerProperty { get; }
}
I couldn't see anything at all about nested interfaces in the Interfaces section of the C# language spec. Are nested interfaces in general just undefined behaviour? If so, is there a reason for that?
Edit
Looking at the language spec for type declarations, here, it says:
A type_declaration can occur as a top-level declaration in a compilation unit or as a member declaration within a namespace, class, or struct.
Interfaces are not specified here, so perhaps this is indeed undefined behaviour. Still, if anyone knows if nested interface types were ever considered and rejected, I'd like to know why.
- See below answers. I was looking at the wrong section of the spec.