Interface vs Multiple Inheritance In C#
Why don't you contain instance of Class A and Class B inside Class C. Use Composition
class C
{
//class C properties
public A objA{get;set;}
public B objeB{get;set;}
}
Then you can access
C objc = new C();
objc.objA.Property1 = "something";
objc.objB.Property1 = "something from b";
check out the article Composition vs Inheritance
EDIT:
if i use Interface instead of Class A, B (In Interface we can not Contains Fields)
Well, interfaces can't contain fields, if you define one, you will get compilation error. But interfaces can contain properties with the exception that you can't specify the access specifiers, as all elements of the interface are considered public
. You can define properties for Interface 'A' and 'B' as:
public interface IA
{
int Property1 { get; set; }
}
public interface IB
{
int Property2 { get; set; }
}
Then you can implement them in the class C like:
public class C : IA, IB
{
public int Property1 { get; set; }
public int Property2 { get; set; }
}
Later you can use them as:
C objC = new C();
objC.Property1 = 0;
objC.Property1 = 0;
Interfaces can have properties but if you also want to use the methods the composition or dependence injection might be required.
Interface A
{
int PropA {get; set;}
}
Interface B
{
int PropB {get; set;}
}
class C : A, B
{
}
// put these statement in some method
C c = new C();
c.PropA = 1;
c.PropB = 2;
Interfaces are not a solution to the lack of Multiple Inheritance. They just don't do the same things. The closest you can get is make C be a subclass of A, and have a property of type B. Perhaps if you tell us what A, B and C should do, we can give an answer that better fits your needs...