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
You cannot do multiple inheritance in C# till 3.5. I dont know how it works out on 4.0 since I have not looked at it, but @tbischel has posted a link which I need to read.
C# allows you to do "multiple-implementations" via interfaces which is quite different to "multiple-inheritance"
So, you cannot do:
class A{}
class B{}
class C : A, B{}
But, you can do:
interface IA{}
interface IB{}
class C : IA, IB{}
HTH
Nope, use interfaces instead! ^.^
You may want to take your argument a step further and talk about design patterns - and you can find out why he'd want to bother trying to inherit from multiple classes in c# if he even could
It does not allow it, use interface to achieve it.
Why it is so?
Here is the answer: It's allowed the compiler to make a very reasoned and rational decision that was always going to consistent about what was inherited and where it was inherited from so that when you did casting and you always know exactly which implementation you were dealing with.
You can't inherit multiple classes at a time. But there is an options to do that by the help of interface. See below code
interface IA
{
void PrintIA();
}
class A:IA
{
public void PrintIA()
{
Console.WriteLine("PrintA method in Base Class A");
}
}
interface IB
{
void PrintIB();
}
class B : IB
{
public void PrintIB()
{
Console.WriteLine("PrintB method in Base Class B");
}
}
public class AB: IA, IB
{
A a = new A();
B b = new B();
public void PrintIA()
{
a.PrintIA();
}
public void PrintIB()
{
b.PrintIB();
}
}
you can call them as below
AB ab = new AB();
ab.PrintIA();
ab.PrintIB();