Why is it impossible to inherit an open class from an inner class?

See this code:

internal class c { private int d; } public class a : c { private int b; } 

Why can't I inherit an open class from an inner class? Why does this compiler have this behavior?

+4
source share
4 answers

Because the open class will be visible outside of your current assembly, but the inner one will not. When you withdraw from a class, you can limit visibility even more, because in your case it will make the implementation c available to consumers outside of your assembly, which defeats the goal of creating an inner class.

However, you can use composition instead of inheritance.

+7
source

C # construction principle. A derived class must at least have the same availability as the parent class. In your case, this is not prohibited. Take a look at Eric Lippert's presentation on this conclusion of the public class from the inner class

+3
source

Because the “public class” is more “visible” than the “inner class”.

C # has a visibility protection layer that prevents this.

+2
source

Inner classes can only be accessed within the Assembly in which they are defined. When a public class a inherits from an inner class, in reality attempts to make the inner class public.

To avoid this, encapsulate the inner class in an open class.

+2
source

Source: https://habr.com/ru/post/1491309/


All Articles