"Go" writing high-quality Code 157 recommendations for improving C # programs--recommendation 96: Members should prioritize exposing base types or interfaces

Source: Internet
Author: User

Recommendation 96: Members should prioritize exposing base types or interfaces

Type members will allow the type to support more applications if the public and the type or interface are given precedence.

The most typical example in the FCL is the functional operation of the collection. The set is divided into several types according to function, such as list<t>, dictionary<tkey,tvalue>, hashset<t>, etc. Take one of the simplest operations of empty (emptying the collection) as an example. This feature requires that we delete all the elements in the collection and then return a clean collection. If you do not return a base type or interface, you are required to implement one of these methods for each collection type.

Microsoft implemented such a static type enumerable in the FCL, which has a static method:

 Public Static Ienumerable<tresult> empty<tresult>() {     return emptyenumerable<tresult> . Instance;   }

Because the generic interface IEnumerable is used, all of the collection subclasses can now implement their own empty method. We should appreciate the benefits of this programming model and use it flexibly in our own projects.

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 96: Members should prioritize exposing base types or interfaces

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.