Does C# support multiple inheritance?

后端 未结 17 783
傲寒
傲寒 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:07

    C# does not support multiple inheritance of classes, but you are permitted to inherit/implement any number of interfaces.

    This is illegal (B, C, D & E are all classes)

    class A : B, C, D, E
    {
    }
    

    This is legal (IB, IC, ID & IE are all interfaces)

    class A : IB, IC, ID, IE
    {
    }
    

    This is legal (B is a class, IC, ID & IE are interfaces)

    class A : B, IC, ID, IE
    {
    }
    

    Composition over inheritance is a design pattern that seems to be favorable even in languages that support multiple inheritance.

提交回复
热议问题