08
Apr
2011

Multiple inheritance in c sharp

Hi there!

all of us .net programmers know that there isn’t a built in way to create multiple inheritance in c#,
well – i needed such. i know some will argue and say this pattern is a no no, and i should have used other methods to create what i wanted,
but for me – it make perfect sense to do so.
am sharing with you the simple way i did so, if it is useful for one of you, i’m glad !

?View Code CSHARP
public class Multiple  where TType1 : new()
where TType2 : new()
{
static TType1 base1;
static TType2 base2;
 
public Multiple()
{
base1 = new TType1();
base2 = new TType2();
}
 
public static implicit operator TType1(Multiple c)
{return base1;}
public static implicit operator TType2(Multiple c)
{return base2;}
}
 
class Foo{}
class Bar{}
class FooBar : Multiple
{}
 
//then use as
 
FooBar foobar=new FooBar();
((Foo)foobar).FooFunciton();
((Bar)foobar).BarFunction();
foobar.FooBarFunction();

attached is a visual studio 2010 sample code: MinheritCSharp.zip

Cheers,

Ady

Comments
Posted in ASP.NET, c#, ,