views:

171

answers:

2

Is it possible for a class to inherit from a nested class, or to implement a nested interface in C#?

class Outer : Outer.Inner {
    class Inner { ... }
    ...
}
A: 

As long as the nested type is visible in your scope and not sealed, then yes.

Edit 2: Do not take this post as any commentary on whether or not you should OR shouldn't do this, I am only stating that it is allowed. :)

Edit: You cannot derive from a type nested within itself, but you can implement an interface declared nested in a base type:

public class Base
{
    public interface ISomething
    {
    }
}

public class Derived : Base, Base.ISomething
{
}
280Z28
+2  A: 

In the way that you wrote, no (see this). But if your inner interface is in another class, then you can.

public class SomeClass : SomeOtherClass.ISomeInterface {
    public void DoSomething() {}
}

public class SomeOtherClass {
    public interface ISomeInterface {
        void DoSomething(); 
    }
}
Fernando
I don't see why this can't be done, but that is the fact!! Anyway... now I am very sad with C#.
Miguel Angelo