Multiple inheritance in C#

Multiple inheritance 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

Multiple Inheritance In C# Using Interfaces

C# does not support multiple inheritance , because they reasoned that adding multiple inheritance added too much complexity to C# while providing too little benefit. 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".

WRONG:

class NewClass : FirstClass, SecondClass { }
C# doesn't support Multiple inheritance

The above code is wrong in C# because the new class is created from class "FirstClass" and class "SecondClass". The reason is that C# does not allows multiple inheritance . There are several reasons for this implementation, but it mostly comes down to that multiple inheritance introduces much more complexity into a class hierarchy.

.Net Interface

.Net Interface

An Interface looks like a class, it contain only the declaration of the members but has no implementation. It provides a contract specifying how to create an Object, without caring about the specifics of how they do the things. An Interface is a reference type and it included only the signatures of methods, properties, events or indexers. In order to implement an interface member, the corresponding member of the implementing class should be public , non-static , and have the same name and signature as the interface member. Click here to know more about.... C# Interfaces .