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 { ... }
    ...
}
1

2 Answers 2

2

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(); 
    }
}
Sign up to request clarification or add additional context in comments.

1 Comment

I don't see why this can't be done, but that is the fact!! Anyway... now I am very sad with C#.
1

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
{
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.