Personal Summary:. NET extension methods

Source: Internet
Author: User

. Net extension Methods

Profile:

I believe a lot of people like me, are consciously using the "extension method" this technology, but basically do not have to manually write an extension method to develop, here I do the "extension method" Summary and simple small application.

Body:

1. Conceptual stuff:

first of all, the extension method is essentially a static method , and after compiling, it is actually implemented by invoking static methods with static classes. The instance method can be overloaded, but the instance method is called first. and, extension methods defined on the parent class can be used in subclasses.

2. Actual code:

<summary>
The first argument of this method must begin with this and specify which type the method is extended from
</summary>
public static Class Myexpandclass
{
public static string Myexpandfunc (this int inputparam)
{
Return "Inputparam:" + inputparam.tostring ();
}
}

static void Main (string[] args)
{
int intput = 19;
String Returnvalue=intput.myexpandfunc ();
Console.WriteLine (returnvalue);
}

results :inputparam:19

In this way, an extension method Myexpandfunc() is completed under the INT type.

3. Extension content:

As you can see, the return value is string, extended on the int type, then you can imagine that depending on the return value, but also write a bunch of different extension methods, according to different extension types and write a bunch of different extension methods, it is obviously unreasonable. But Microsoft offers a good solution to the demo: As can be seen, a set of extension methods in the Ienumerable<t> generic interface should now know the solution to the problem raised above. 

Personal Summary:. NET extension methods

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.