I have a set of Class A and Class B both have Some properties. and another Class C which has their own properties.
Whenever i create a instance of class C i want to access all properties of all three classes with objClassC.
How can i Achieve this in C#?
I m facing two problem :-
- I can not inherit both the classes A, B in Class C (C# doesn’t Support Multiple Inheritance)
- if i use Interface instead of Class A, B (In Interface we can not Contains Fields)
Why don’t you contain instance of Class A and Class B inside Class C. Use Composition
Then you can access
check out the article Composition vs Inheritance
EDIT:
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:Then you can implement them in the class C like:
Later you can use them as: