I would like to achieve this in C#
(Pseudocode)
class A;
class B : A;
class C : A, B;
...
A ac = (A)c;
...
B bc = (B)c;
Is this possible?
I would like to achieve this in C#
(Pseudocode)
class A;
class B : A;
class C : A, B;
...
A ac = (A)c;
...
B bc = (B)c;
Is this possible?
No. C# does not support multiple inheritance of classes.
A class may inherit from one class, and may also implement multiple interfaces.
It is not possible in C#, but, you should also think if this is the scenario you really want.
Is C really an A and a B ? Or does C has an A and a B. If the latter is true, you should use composition instead of inheritance.
You do not need multiple inheritance in this particular case: If class C
inherits only from B
, any instance of class C
can be cast to both B
and A
; since B
already derives from A
, C
doesn't need to be derived from A
again:
class A { ... }
class B : A { ... }
class C : B { ... }
...
C c = new C();
B bc = (B)c; // <-- will work just fine without multiple inheritance
A ac = (A)c; // <-- ditto
(As others have already said, if you need something akin to multiple inheritance, use interfaces, since a class can implement as many of those as you want.)