Does C# support multiple inheritance?

后端 未结 17 736
傲寒
傲寒 2020-11-27 05:34

A colleague and I are having a bit of an argument over multiple inheritance. I\'m saying it\'s not supported and he\'s saying it is. So, I thought that I\'d ask the brainy

17条回答
  •  一生所求
    2020-11-27 06:08

    Multiple inheritance allows programmers to create classes that combine aspects of multiple classes and their corresponding hierarchies. For ex. the C++ allows you to inherit from more than one class

    In C#, the classes are only allowed to inherit from a single parent class, which is called single inheritance. But you can use interfaces or a combination of one class and interface(s), where interface(s) should be followed by class name in the signature.

    Ex:

    Class FirstClass { }
    Class SecondClass { }
    
    interface X { }
    interface Y { }
    

    You can inherit like the following:

    class NewClass : X, Y { } In the above code, the class "NewClass" is created from multiple interfaces.

    class NewClass : FirstClass, X { } In the above code, the class "NewClass" is created from interface X and class "FirstClass".

提交回复
热议问题