8

Exactly as the title says.

Is it a restriction placed by the C# compiler or does the CLR fundamentally prohibit it?

RichK
  • 11,318
  • 6
  • 35
  • 49
  • 1
    FWIW (although old): http://blogs.msdn.com/b/csharpfaq/archive/2004/03/07/why-doesn-t-c-support-multiple-inheritance.aspx – lc. Aug 07 '13 at 15:57

3 Answers3

8

Both.

The C# language, which is not directly tied to the CLR (i.e. Mono AOT) does not allow multiple inheritance.

The CLR type system, which supports languages other than C#, also does not support multiple inheritance.

Robert Harvey
  • 178,213
  • 47
  • 333
  • 501
SLaks
  • 868,454
  • 176
  • 1,908
  • 1,964
  • 1
    Hi @SLaks, I researched a bit and couldn't find anywhere on the internet about that both C# compiler and both the CLR type system enforce not using multiple inheritance. Do you mind giving a reference to a formal source like msdn? Thanks in advanced, – Ami Jun 19 '17 at 14:50
  • 1
    @Ami: The entire system is design around each class having a single base class. Look at any part of the documentation or syntax around that. – SLaks Jun 19 '17 at 16:38
  • 1
    I know that C# is designed that all classes have a single base class. Though, the only thing I can't verify is whether the CLR type system enforce not using multiple inheritance additionaly to the C# compiler (and probably other compilers). From searching online I could find that CLR does support this feature of multiple class inheritance. So Im a little confused. Does the CLR check if there's a multiple base class and block it, or it isn't? – Ami Jun 20 '17 at 17:31
  • @Ami: https://stackoverflow.com/a/6258236/34397 https://blogs.msdn.microsoft.com/csharpfaq/2004/03/07/why-doesnt-c-support-multiple-inheritance/ – SLaks Jun 30 '17 at 18:29
2

The CLR prohibits it. If it was only the compiler, you could use Reflection to overcome it at runtime. Multiple inheritance (other than by interface) is in direct violation of the Type system of the CLR.

Haney
  • 32,775
  • 8
  • 59
  • 68
  • Without language support, you would not be able to use reflection to create types that had multiple inheritance. You would have to write the IL manually. – mageos May 19 '17 at 21:33
  • @mageos: Writing IL manually (or rather emitting it dynamically) is something I, and probably at least a few others, do semi-regularly to overcome limitations in C#. – caesay Feb 01 '18 at 18:14
2

There are at least three levels:

  • .NET library: the type system of the .NET doesn't support it (look at the Type.BaseType property... It's a Type, not a Type[], so not even future support)

  • IL Language: I don't know, but probably not, otherwise the Type type would be different (because the IL language was built together with everything else)

  • C# compiler: no, because the C# is the prime-choice language of .NET and must show everything (nearly everything) that can be done in .NET without creating too many constructs not emulable through the "plain" .NET . Syntactic sugar like LINQ and object initializers are one thing (easily emulable by other languages), a parallel type system not :-)

xanatos
  • 109,618
  • 12
  • 197
  • 280