C # When to use class inheritance (base class) and interface (Interface) during class design)

Source: Internet
Author: User

 

(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 :)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.