C # Analysis of extension method calls

Source: Internet
Author: User

Through the previous two sections, we learned that the extension method is a special static method. The definition method of the extension method is similar to that of the general static method, the only difference is that the keyword "this" must be added before the first parameter as the modifier.

The call method of an extension method is the same as that of an instance method of the extension type. Since the extension method can be called like an instance method on the extension type, how does the compiler decide whether to use an extension method. For this problem, the compiler works according to the following process: When the compiler finds that an expression seems to use an instance method, however, no instance method compatible with this method is found in all the imported namespaces and the current namespaces, you can find an appropriate extension method in all the imported namespaces and the current namespaces. Here, we can remember two points: (1) the instance method is used before the extension method; (2) the search extension method ranges from all the namespaces imported to the current namespace, this is easy to understand.

In fact, there is another question that needs to be elaborated. How can the compiler choose between multiple extension methods in the search results. In fact, the compiler only needs to find the most appropriate method in the overloaded method.


The following is a simple example. We hope this example can give you a better understanding of the calling of extended functions.

Step 1: Create the extension method Print () in namespace ConsoleApplication2. Use this extension method to complete simple printing operations. The extension method is located in the static class ExtendFunction.

[Csharp]
Namespace ConsoleApplication2
{
Public static class ExtendFunction
{
Public static void Print (this object o)
{
Console. WriteLine (o );
}
}
}

Namespace ConsoleApplication2
{
Public static class ExtendFunction
{
Public static void Print (this object o)
{
Console. WriteLine (o );
}
}
}

Step 2: Create a class TestClass under ConsoleApplication2 and use the Print () method in this class to Print the class.

[Csharp]
Namespace ConsoleApplication2
{
Public class TestClass
{
Private object priStr;
Public TestClass (object o)
{
This. priStr = o;
}
Public void Print ()
{
Console. WriteLine (this. priStr );
}
}
}

Namespace ConsoleApplication2
{
Public class TestClass
{
Private object priStr;
Public TestClass (object o)
{
This. priStr = o;
}
Public void Print ()
{
Console. WriteLine (this. priStr );
}
}
}

Step 3: implement the following code for calling the Main function:

[Csharp]
Namespace ConsoleApplication2
{
Class Program
{
Static void Main (string [] args)
{
String s = "hell, world! ";
Int I = 32;
TestClass tc1 = new TestClass (s );
TestClass tc2 = new TestClass (I );
// Call the instance method
Tc1.Print ();
// Static method call
S. Print ();
// Call the instance method
Tc2.Print ();
// Static method call
I. Print ();
}
}
}

Namespace ConsoleApplication2
{
Class Program
{
Static void Main (string [] args)
{
String s = "hell, world! ";
Int I = 32;
TestClass tc1 = new TestClass (s );
TestClass tc2 = new TestClass (I );
// Call the instance method
Tc1.Print ();
// Static method call
S. Print ();
// Call the instance method
Tc2.Print ();
// Static method call
I. Print ();
}
}
}
Step 4: Execute the program and get the following results:

 

In fact, we should have predicted this result for a long time. Because the extension method Print () in this instance is equivalent to the Print () method provided by the TestClass class in terms of functions.

Through this example, we can further understand that the call method of the extension method is the same as that of the instance method of the extension type, such as "tc1.Print ()" and "s. print ()".

More importantly, we track the code flow direction by setting the breakpoint and one-step debugging before the method, so that we can further realize that the instance method is actually used before the extension method. So how can I determine this? Now I will explain my practices: (1) first, set a breakpoint before the Print () Extension Method and the Print () member METHOD OF THE TestClass class; (2) Next, start single-step debugging and track the code flow. Note that the TestClass instance method Print () is used to call "tc1.Print ()" and "tc2.Print (); while the function calls "s. print () "and" I. print () "calls the extension method Print (); (3) then, comment out the Member method Print () of the TestClass class, and start single-step debugging again to track the direction of the Code flow, found: This function calls "tc1.Print ()", "tc2.Print ()", "s. print () "," I. print () "calls the extension method Print (), which completes the function call according to the call process of" first instance method and then extension method. Of course, the running results of this program are also different from those of the previous one. For specific results, see:

 

Why? The reason is simple. Do you say yes? Think about it.

Finally, add the following code to the static class ExtendFunction:

[Csharp]
Public static void Print (this object o, bool isPrint)
{
If (isPrint = true)
{
Console. WriteLine (o );
}
}

Public static void Print (this object o, bool isPrint)
{
If (isPrint = true)
{
Console. WriteLine (o );
}
}
 

Start single-step debugging again and find that the function call "s. print () "and" I. print () "calls an extension method that has the extension method before the function" public static void Print (this object o, instead of the extension method before the function "public static void Print (this object o, bool isPrint. It seems that when there are multiple applicable (only method names are the same) extension methods, the compiler does find the most appropriate one to execute in the overloaded method.

 

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.