Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why is it not possible to inherit a public class from an internal class? [duplicate]

Tags:

c#

See this code:

internal class c 
{
    private int d;
}

public class a : c
{
    private int b;
}

Why can I not inherit a public class from an internal class? Why does the compiler have this behavior?

like image 636
Yazdan Bayati Avatar asked Dec 26 '22 00:12

Yazdan Bayati


2 Answers

Because the public class would be visible outside your current assembly, while the internal one isn't. When deriving from a class you can only restrict visibility further, because in your case it would make the implementation of c available to consumers outside your assembly which kind of defeats the purpose of making the class internal in the first place.

You can use composition instead of inheritance, though.

like image 173
Joey Avatar answered Jan 11 '23 23:01

Joey


C# design principle. Derived class should atleast have same accessibility as the parent class. In your case it is not hence not allowed. Take a look at Eric Lippert's view on this deriving public class from an internal class

like image 38
Ehsan Avatar answered Jan 11 '23 23:01

Ehsan