(Www.free56.cn, based from 'clr via C #')
The use of base class and interface is sometimes easy to judge and sometimes difficult to distinguish.
1) Main differences between the two: base class reflects the relationship between "is a", while interface reflects the relationship between "can do. Because C # does not allow multi-inherit, do not use the base class if the derived class does not correctly reflect the relationship of the IS-A; if different classes or objects (types) contain the same can-do function, the interface is used. In addition, value type can only use interfaces and cannot use base class;
2) compared with interfaces, the use of base class is somewhat:
(I) Easy to use: the base class provides a large number of ready-made methods and functions that can be directly used by subclass. When using interfaces, you must define the members specified by interfaces;
Ii) Consistent implementation, it is impossible to achieve 100% accuracy, which leads to the generation of bugs. Instead, you can use the base class method to define a base class that can be correctly executed, the appropriate modification in the derived class has reached the final requirement, which will undoubtedly make the program more stable and have a lower chance of problems;
Iii) Versioning: if a new method is added to the base class, the inherited subclass may use the newly added methods in the base class without compilation; it is not so easy to use interfaces. After a new declaration is added to an interface, the new methods must be implemented in the subclass.
3) Interface Usage: for classes that provide relatively stable (Consistent) operations, it is best to define a unified interface for them. For example, all aggregate objects (lists) in your class can add, insert, or delete projects in a list, we can let them all inherit from the ilist <t> interface. In this case, you can add, insert, or delete your classes, you don't need to know their specific classes, because you know they all implement the ilist <t> interface method.
4) Of course, there is no difference between defining interfaces and using class inheritance. For example, to implement the Compare function, FLC provides the abstract classes of icompareer <t> and comparer <t> for inheritance. Look at your habits :)