c#3.0 extension Method (Extension Methods)

Source: Internet
Author: User
Tags new features readline requires static class

This morning I saw this article at the MSDN site: The Evolution of C # 3.0 LINQ and its impact on C # design. From this article we can see clearly that the new features that are added to C # 3.0 can be considered for the development of LINQ, some of the new features that are created for LINQ, and of course those features that can be used by non-LINQ-related features. The development of LINQ has led to the emergence of a series of new features of c#3.0.

Come back to our topic, extension methods, first look at one of the simplest code examples:

The definition of extension methods requires three parts: 1, Static class (private public Can), 2, static method (private public all Can), 3, first function parameter before this (must be before the first parameter)

Namespace Hongjun.guo
{
Static Class Myextensionmethods
{
internal static void Print (This object s)
{
Console.WriteLine (s);
}
}
}

The use of extension methods requires attention: Using the namespace you define.
Using Hongjun.guo;
static void Main (string[] args)
{
Object o = "Dsdgs";
O.print ();
}

With such a simple code, we can easily add a lot of our own extra functionality to some of the less Open-source third-party controls.

The use of extension methods, sometimes not so simple, we look at some special cases, through these special case analysis, we can more in-depth understanding of extension methods:

Case one: The process logic of extending the method with the same method as the original class.
Scenario: We are extending a method for a third party's class that has no open source components, such as method: Print. After a while, the third party component releases a new version of the class, which adds the Print method. What effect will this be??
Problem Demo Code:
Namespace Hongjun.guo
{public class MyClass
{
public void Print ()
{
Console.WriteLine ("* * *");
}
} Static Class Myextensionmethods
{
internal static void Print (this MyClass s)
{
Console.WriteLine ("Haha" + s.tostring ());
} }
}
Invocation Example:
Using Hongjun.guo;
static void Main (string[] args)
{
MyClass o = new MyClass ();
O.print (); Console.ReadLine ();
}

What results will we see at this time??

The answer: see is * * * that is, the extension method and the method of the class conflict, compile use does not report any errors, at this time the class method of precedence is the highest, when we use the method of the class, rather than the extension method. Analysis: We compile the above two sets of code, and then decompile into IL, we can see that, in fact, the extension method does not exist on the IL layer. The extension method is actually when the compiler calls a method of a class, go to this class first, if there is this method, then call, if not found, according to the namespace of the reference, then find the extension method (static class static method). Find, use, can not find the compiler error of course. Based on the results of this analysis, we can understand the results of the above problem processing.

Case two: Nesting of extension methods such as we have the following extension methods.

Namespace Hongjun.guo
{
Static Class Myextensionmethods
{
public static int Test01 (this int i)
{
return I * 3;
public static int Test02 (this int i)
{
return i + 5;
}
}
}

Here is the invocation example:

static void Main (string[] args)
{
int mm = 7;
Console.WriteLine (MM. TEST01 (). Test02 ());
Console.WriteLine ("* * *");
Console.WriteLine (MM. Test02 (). TEST01 ()); Console.WriteLine ("* * *");
Console.WriteLine (myextensionmethods.test02 (myextensionmethods.test01 (mm)));

Console.ReadLine ();
}
Q, what is the value of the call's display result?
Answer: Display in turn: 26,36,26
Analysis: MM. TEST01 (). Test02 () This line of code compiles the following code: myextensionmethods.test02 (myextensionmethods.test01 (mm)) The two lines of code are exactly the same in the compiled IL.

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.