C #3.0 -- Extension Method

Source: Internet
Author: User

The extension method is a new feature of C #3.0. It allows us to expand (that is, add) the existing instance methods without changing the source code, it also provides us with another method to expand the type of behavior (other methods are inheritance, combination, reflection ).

Here is a sample code:

 

Class Program
{
Static void Main (string [] args)
{
String s = "hello ";
S. SayTo ("world ");
Extensions. SayTo (s, "world ");

}
}

Public static class Extensions
{
Public static void SayTo (this string s, string name)
{
Console. WriteLine (s + name );

}
}

 

 

In the Extensions class, we define an extension method SayTo () of the string type, from which we can see several features of the extension method definition:

 

 

1. The first parameter of the extension method has a this keyword, which indicates that this is an extension method.

2. The first parameter of the extension method is the type to be extended. In this example, the string type is extended.

3. Extension methods can only be defined in static classes, while static classes can only declare static methods. Therefore, extension methods are static.

 

We use this extension method in the Program class. We can note that we call a static method by calling the instance method. We should note the following aspects when using the extension method:

 

1. When you call an extension method using an instance method of the type, the first parameter does not need to be input. If you enter the parameter, an error is returned. When compiling the code, the compiler actually calls s. sayTo ("world") is converted to Extensions. sayTo (s, "world"), the essence of an extension method is to change the call of an instance method to a call of a static method in a static class during the compilation period.

2. The extension method can also be called in static mode. Extensions. SayTo (s, "world") can be used to replace s. SayTo ("world"). The effect is the same. Let's take a look at the IL code.

L_0008: ldstr "world"

L_000d: call void Extensions. Extensions: SayTo (string, string)

The same IL code is generated when two statements are called.

3. The extension method has its own priority. The existing instance method has the highest priority, followed by the extension method in the nearest namespace, and finally the Extension Method in the distant namespace.

4. When designing classes, do not think that they can be expanded in the future. It is not advisable to take the design seriously. The extension method is generally used to expand some code that is not easy to modify.

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.