A C# class can inherit directly from only one class, which of course can inherit from others. A C# class cannot inherit directly from more than one class.
The workaround for this is C# interfaces. A class can inherit from more than one interface.
interface is a keyword that would replace the word class in what resembles an abstract class definition. All methods declared in an interface are abstract by default.
This means that in an interface the methods are only defined with a method header, such as int myMethod(int myInt).
Data members can be included but the values must be assigned or retrieved with get and set properties. The compiler will generate an error (for example, Interfaces cannot contain fields), if you try to create the same way you would create class variables. Thus, something like int myInt; won't fit into a interface definition but int myInt { set; get; } will.
I could be wrong, but it appears that you can't really store anything in myInt when you define it as part of the interface. You will have to create a variable in the class defintion that inherits from the interface if you want to store a value in an object.
I wish I understood the reasons for this approach to multiple inheritance. I'm going to guess that it was a compromise between flexibility and compiler efficiency.
No comments:
Post a Comment