"Go" writing high-quality Code 157 recommendations for improving C # programs--Recommendation 102: Differentiating between interfaces and abstract classes of applications

Source: Internet
Author: User

Recommendation 102: Distinguish between interfaces and abstract class applications

There are some obvious differences between interfaces and abstract classes:

    1. Interfaces support multiple inheritance, and abstract classes are not.
    2. Interfaces can contain signatures for methods, properties, indexers, events, but not implementations, and abstract classes can.
    3. Interface after adding a new method, all inheritors must be refactored, otherwise the compilation does not pass, and abstract classes are not required.

These differences result in different scenarios for both:

    1. An abstract class is used if the object has multiple versions that are close in function and closely related.
    2. If the relationship is not close, but several features have a common declaration, the interface is used.
    3. Abstract classes are suitable for situations where rich functionality is provided, and interfaces are more inclined to provide a single set of functions.

From a certain point of view, the abstract analogy interface has more reusability of code. Subclasses can have a common behavior without having to write code.

Another benefit of adopting an abstract class is that if you add a method to the base class, then all subclasses of that base class will naturally have this extra method, and the interface does not. If an interface adds a method, all subclasses must be modified. So, once the interface is designed, it should be constant. Abstract classes can add functionality as the version is upgraded.

The role of an interface is more likely to indicate that a type has some or some function. Interfaces are only responsible for declarations, and abstract base classes are often responsible for implementation.

The responsibilities of the interfaces must be single, and the methods in the interfaces should be as concise as possible.

We should be in the actual design, take into account the type of application, according to its function to complete, to decide whether to design the type as an abstract class or interface.

Turn from: 157 recommendations for writing high-quality code to improve C # programs Minjia

"Go" writing high-quality Code 157 recommendations for improving C # programs--Recommendation 102: Differentiating between interfaces and abstract classes of applications

Related Article

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.