Does C# support multiple inheritance?

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

    Sorry, you cannot inherit from multiple classes. You may use interfaces or a combination of one class and interface(s), where interface(s) should follow the class name in the signature.

    interface A { }
    interface B { }
    class Base { }
    class AnotherClass { }
    

    Possible ways to inherit:

    class SomeClass : A, B { } // from multiple Interface(s)
    class SomeClass : Base, B { } // from one Class and Interface(s)
    

    This is not legal:

    class SomeClass : Base, AnotherClass { }
    
    0 讨论(0)
  • 2020-11-27 06:03

    I recently somehow got to the same mindset, inheriting two classes into a class and ended up on this page (even though i know better) and would like to keep reference to the solution i found perfect in this scenario, without enforcing implementation of interfaces

    My solution to this problem would be splitting up your data into classes that make sense:

    public class PersonAddressSuper
    {
        public PersonBase Person { get; set; }
        public PersonAddress Address { get; set; }
    
        public class PersonBase
        {
            public int ID { get; set; }
            public string Name { get; set; }
        }
        public class PersonAddress
        {
            public string StreetAddress { get; set; }
            public string City { get; set; }
        }
    }
    

    Later on in your code, you could use it like this:

    Include Both Parts, Base & Address

    PersonAddressSuper PersonSuper = new PersonAddressSuper();
    PersonSuper.PersonAddress.StreetAddress = "PigBenis Road 16";
    

    Base Only:

    PersonAddressSuper.PersonBase PersonBase = new PersonAddressSuper.PersonBase();
    PersonBase.Name = "Joe Shmoe";
    

    Address Only:

    PersonAddressSuper.PersonAddress PersonAddress = new PersonAddressSuper.PersonAddress();
    PersonAddress.StreetAddress = "PigBenis Road 16";
    
    0 讨论(0)
  • 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.

    0 讨论(0)
  • 2020-11-27 06:08

    Multiple inheritance is not supported in C#.

    But if you want to "inherit" behavior from two sources why not use the combination of:

    • Composition
    • Dependency Injection

    There is a basic but important OOP principle that says: "Favor composition over inheritance".

    You can create a class like this:

    public class MySuperClass
    {
        private IDependencyClass1 mDependency1;
        private IDependencyClass2 mDependency2;
    
        public MySuperClass(IDependencyClass1 dep1, IDependencyClass2 dep2)
        {
            mDependency1 = dep1;
            mDependency2 = dep2;
        }
    
        private void MySuperMethodThatDoesSomethingComplex()
        {
            string s = mDependency1.GetMessage();
            mDependency2.PrintMessage(s);
        }
    }
    

    As you can see the dependecies (actual implementations of the interfaces) are injected via the constructor. You class does not know how each class is implemented but it knows how to use them. Hence, a loose coupling between the classes involved here but the same power of usage.

    Today's trends show that inheritance is kind of "out of fashion".

    0 讨论(0)
  • 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".

    0 讨论(0)
  • 2020-11-27 06:09

    Simulated Multiple Inheritance Pattern
    http://www.codeproject.com/KB/architecture/smip.aspx enter image description here

    0 讨论(0)
提交回复
热议问题