Rapid understanding of extension methods

Source: Internet
Author: User

---restore content starts---

Today to record your understanding of the extension method, the extension method is to implement a function but in the original class or assembly does not exist in the implementation of the method or encapsulation, and then derive the extension method. But creating a new class to encapsulate a way to implement this functionality can also be achieved with the use of an inheritance mechanism, and why is there a derivative extension method?

    1. The first is a more generic approach and is usually the most used, but the readability of the code is not as good as the extension method compared to the extension method.
    2. The way to inherit is a good extension, but sometimes it is not necessarily perfect, for example: a. Each inheritance produces a new class, and a corresponding cast (string=>mystring) is required for use, B. Some classes themselves are designed as sealed classes and are not allowed to be inherited. The example string used above is not allowed to be inherited, so the second scenario is not feasible. at this point, the third method of solution expansion comes in handy.

Therefore, the extension method can be a perfect solution to the above two kinds of mechanisms produced or left behind problems and shortcomings, to learn more about the use of extension methods!

Declaring an extension method must conform to the following characteristics:

    1. The method must be declared in a static class and the method type is a static method
    2. The first parameter type of a method is an extensible type
    3. The first parameter is required to add the This keyword

Write a demo to see:

namespacezhengze{ Public Static classMyarr { Public Static intGetadd ( This int[] arr) {            intMax =0;  for(inti =0; I < arr. Length; i++) {Max+=Arr[i]; }            returnMax; }    }}

Then call the method:

namespace zhengze{    class  program    {        publicstaticvoid Main (string[] args)        {                        int[] arr = {1,3,5,7  };            Console.WriteLine (arr. Getadd ());     }}}

So that one of my simple extension methods is done. How to identify an extension method:

    1. The first method of the parameter type is this
    2. Method icon marked with subscript arrow

The above content is partly from the http://home.cnblogs.com/u/codelir/blog. This chapter is only for your own understanding of the review to deepen the memory, there is insufficient place please point out! ---Dedicated to the second time

---restore content ends---

Rapid understanding of 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.