简体   繁体   English

为什么我不能从私有类/接口继承?

[英]Why can't I inherit from a private class / interface?

Why is this illegal in C#? 为什么在C#中这是非法的?

class Foo: Foo.BaseFoo //Circular base class dependency compile time error
{
   private class BaseFoo {...}

   ...
}

I'm not arguing when this could be useful or not, but I'd like to know what are the reasons that would disallow such code to compile. 我不是在争论什么时候有用,但是我想知道是什么原因阻止了这些代码的编译。 A similar restriction happens with private interfaces. 私有接口也有类似的限制。

UPDATE UPDATE

Seeing that its a duplicate I'll center the question more in why this isn't valid with interfaces which seems more useful? 看到它是重复的,我将集中更多的问题,为什么这对于看起来更有用的接口无效?

And, what's more, why does it seem to be legal with the Roslyn preview as shown here 而且,更重要的是,为什么它似乎是法律与罗斯林预览如图所示这里

This is not a problem of the access modifier -that the class is private. 这不是访问修饰符的问题-该类是私有的。 This is a circular base class dependency. 这是循环基类依赖项。 You try to define a class called Foo that inherits a nested class called BaseFoo . 您尝试定义一个名为Foo的类,该类继承了一个称为BaseFoo的嵌套类。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何实现接口并从基类继承? - How can I implement an interface and inherit from a base class? 为什么一个结构体不能继承自 class,而所有结构体都继承自 class“对象”? - Why can't a struct inherit from a class, but all structs inherit from the class "object"? 如何创建一个 List(of T),其中 T 是 (T) 的接口,List 的内容继承了一个实现 T 接口的抽象类 - How can I create a List(of T) where T is an interface of (T) and the contents of the List inherit an Abstract class that implements the interface of T 如何使一个类既实现接口又从另一个类继承 - How can I make a class both implement an Interface and inherit from another class 为什么AC#类可以隐式和明确地从一个接口继承? - Why Can A C# Class Inherit From One Interface Both Implicitly and Explicitly? 没有完全理解为什么我会在类中有一个接口,而不是从中继承 - not fully grasping why i'd have an interface in a class, rather than inherit from it 在C#中,一个类可以继承自另一个类和接口吗? - In C#, can a class inherit from another class and an interface? 从通用类的Interface继承 - Inherit from Interface in generic class 为什么不能继承static类? - Why can't I inherit static classes? 为什么我不能继承此变量? - Why can't I inherit this variable?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM