开发者

C# inherit from nested class within itself

开发者 https://www.devze.com 2022-12-18 15:13 出处:网络
Is it possible for a class to inherit from a nested class, or to imple开发者_StackOverflow社区ment a nested interface in C#?

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

精彩评论

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