开发者

C# inherit from nested class within itself

Is it possible for a class to inherit from a nested class, or to imple开发者_StackOverflow社区ment a nested interface in C#?

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


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(); 
    }
}


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

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜