Improve C # code 22: replace inheritance by defining and implementing Interfaces

Source: Internet
Author: User

 

1.The base class describes and implements a group of related types sharing behavior. An interface defines a group of atomic functions for other unrelated types. Both of them are useful, but their use is different. An interface is a contract-based design: a type that implements an interface must provide the method implementation agreed in the interface. Abstract base classes provide a shared abstraction for a group of related types. Use the class hierarchy to define related types, use interfaces to expose functions, and implement these interfaces for different types.

 

2.Sometimes, using interfaces can also help us avoid the cost of struct unpacking. When we pack struct, The boxed object actually supports all interfaces supported by struct. When you use an interface pointer to access the struct, you do not have to unpack it to access internal data.

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.