How did C#'s lack of multiple inheritance lead to the need for interfaces?

后端 未结 4 1065
星月不相逢
星月不相逢 2020-12-23 21:17

In The C# Programming Language Krzysztof Cwalina states in an annotation:

we explicitly decided not to add support for multiple inheritance [...] th

4条回答
  •  盖世英雄少女心
    2020-12-23 22:02

    I think Cwalina's language is a bit strong, and not entirely accurate regarding the history.

    Remember that interfaces were around before C#, so to say "we had to add interfaces to solve problem x" doesn't sound right to me. I think interfaces would have been there by default - they would have had to be.

    Also, remember that C# is derived largely from C++. Or, at least, the people involved in creating the C# language would have had very strong backgrounds in C++. Multiple Inheritance was an acknowledged nightmare area in C++. Inheriting from multiple classes which themselves may be derived from commonm base classes, working out which implementation would take precedence etc. I mean, it is a very powerful feature, but can undoubtedly lead to complex code.

    I suspect they dropped multiple inheritance from C# because they (the language authors) knew how tangled everything could become and they wanted to avoid that. Bear in mind also that when C# was introduced one of its selling points was its cleanliness (in fairless no cleaner than Java but a lot cleaner than C and C++).

    Incidentally, in 10+ years of C# development I have only wished for multiple inheritance once. A UI component which I wanted to inherit both a visual style and some common behaviour. It didn't take me long to realise that what I was intending to do would have been a pretty awful design in any case.

提交回复
热议问题