Extending an interface by using extension methods

Source: Internet
Author: User

It is unclear how to preheat the extension method.

Recommendation: http://www.cnblogs.com/luluping/archive/2008/05/26/1207530.html

Excerpt: http://www.cnblogs.com/luluping/archive/2008/05/26/1207536.html

1 , the process logic of extending the method with the same name as the original class method 2 , extending method nesting now let's look at the third scenario used by the extension method: Extending the interface scenario using an extension Method Example: we sometimes find an interface that is originally defined, and in the present environment, this interface needs to add another function. With the extension method, we have one more implementation option in this case.
。。。。。。 Analysis of the advantages of doing this:1, if we implement the MyInterface interface of a lot of classes, these classes do not derive relationships, this time we want to add a function on the interface, according to the previous practice, the implementation of this interface class how many, we need to change how many , after using the extension method, we only need to change one place. Reduce the amount of code. If the class that implements this interface is encapsulated in a different component, and some components are difficult to modify for other reasons, extending the interface with an extension method is a good medicine.2. Theextension method is called, provided that the namespace in which the extension method resides is used. If we put the interface with the extension method in the same namespace, the extension method needs to refer to the namespace of the problem can be considered non-existent. Because you want to use this interface, you will inevitably reference this namespace. 3override this function implementation). First of all, there is no conflict between the two, if you are an interface call, it is an extension method, if the implementation of the class call, it is the implementation of the class's own method

1. Class

/// <summary>    ///Interface/// </summary>     Public InterfaceIeatclass {BOOLhaseat (); }    /// <summary>    ///Implementation Class/// </summary>     Public classEatclass:ieatclass { Public BOOLhaseat () {return false; }    }    /// <summary>    ///Extension Classes/// </summary>     Public Static classeatclassextensions { Public Static BOOLHaseat ( ThisIeatclass Eat,inti) {if(I >0)return true; return false; }    }

2. Call

New Eatclass (); var res = Eat.haseat (); // Output False var res1 = eat.haseat (1); // Output True

3. If the extension method is under a different namespace

Code:

namespacecompany.business.ext{/// <summary>    ///Extension Classes/// </summary>     Public Static classeatclassextensions { Public Static BOOLHaseat ( ThisIeatclass Eat,inti) {if(I >0)return true; return false; }    }}

Add a new namespace when calling

Using Company.Business.Ext;

===================================

Under the different namespaces this problem, toss the morning, the foundation is too poor, continue to work ...

Extending an interface by using extension methods

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.