Click here to Skip to main content
15,899,937 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi Experts,
I have question in C#,we all know that in C# everything is inherited from System.Object.
i.e Object is mother of every class.But Suppose we have code like this.
C#
public class A:Object
{
    //by default Object is inherited in class A or it explicitly 
    //inherited from object
}

public class B :Object, A
{

}


How C# handle in class B driver class from A ?Is C# also inherited from Object in class B drive class?But C# does not support multiple inheritance.

Thanks
Dinesh Sharma

What I have tried:

Hi Experts,
I have question in C#,we all know that in C# everything is inherited from System.Object.
i.e Object is mother of every class.But Suppose we have code like this.
C#
public class A:Object
{
    //by default Object is inherited in class A or it explicitly 
    //inherited from object
}

public class B :Object, A
{

}


How C# handle in class B driver class from A ?Is C# also inherited from Object in class B drive class?But C# does not support multiple inheritance.

Thanks
Dinesh Sharma
Posted
Updated 7-Jun-16 7:39am

1 solution

You are doing it wrong.

  1. There is no multiple inheritance for classes in .NET. There is only the weak form of multiple inheritance: it mean multiple inheritance for interfaces and the "mixed" inheritance for classes: each class cannot have more than one base class, but unlimited number of interfaces.
  2. As System.Object is the top-level base class for all types, you don't need to put it in the inheritance list; all types are derived from this class implicitly. In case of singular inheritance, you can do it, but you don't need to do so.
  3. You are showing the case of normal chain inheritance: A derived from System.Object, B is derived from A, and, indirectly, from System.Object. Even if the top of the inheritance chain wasn't the implicit base class, you would not need to list it in the inheritance list, and, due to lack of multiple inheritance, you are not allowed to do so.

    Her is the simple example:
    C#
    class A { ... } // base class is System.Object
    class B: A { ... } // base classes are System.Object and A
    class C: B { ... } // no need to list A, it is already an indirect base



—SA
 
Share this answer
 
v3
Comments
CPallini 7-Jun-16 16:20pm    
5.
Sergey Alexandrovich Kryukov 7-Jun-16 19:34pm    
Thank you, Carlo.
—SA

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900