A new feature of Visual Studio codename "orcas" is the extender method ).
First, let's take a good look at what is an extension method: literally, it belongs to the category of a method, but why is there an extension above, you can simply understand what "is new. that is,. in the FCL framework of. net, Microsoft engineers have developed some common methods for us to call them directly. This makes us more convenient, it also greatly limits the flexibility of the FLC class. In addition to inheritance, I cannot think of a better method for extending the class. This may be because the extended method is introduced in ipvs. Naturally, to better solve the above problems, of course, you can find some materials about how the extension method promotes the application of LINQ.
Now that you know what the extension method is, let's take a look at how to define or implement the extension method (limited to C #3.0 ). The first is to define a static class (which cannot be instantiated but can only contain static members ). The second is to add a static method (Extension Method) to the static class, and the third is to use the this keyword before the type of the first parameter of the static method. The first parameter is the type supported by the method (or the type to be extended by the method). Of course, there can be other parameters for the extension method, but it can only be placed after the first special parameter ).
The following is an example of implementation (of course very simple). The specific expansion and application scenarios depend on everyone.
Define the extension method in this class library. If your extension method definition and test extension method are defined in different projects, the static class and static method must be declared as public.
/* Function Description: display how to use extend method which is embeded
* As a new function character in the next version
* Visual Studio codename 'codecs'
* Author: haohai Fu
* Date: 2007-6-14
* Version: 1.0
* Details: Make sure that you must define a new public static class in a namespace
* And define a static method as extender, of which the first parameter must be add
* This key word before the type.
*/
Using system;
Using system. Collections. Generic;
Using system. LINQ;
Using system. text;
Namespace classlibrary1
{
Public static class class1
{
Public static string getdate (this string S, string append)
{
Return S + append;
}
}
}
This is a test project.
/* Function Description: This is an application entry. display how to use extend method which is embeded
* As a new function character in the next version
* Visual Studio codename 'codecs'
* Author: haohai Fu
* Date: 2007-6-14
* Version: 1.0
* Details: please pay attention to how to call the extender method.
*/
Using system;
Using system. Collections. Generic;
Using system. LINQ;
Using system. text;
Namespace consoleapplication2
{
// Using leleapplication1;
Using classlibrary1; // select the project tab by adding a reference (why not select the first tab? If you are interested, refer to Microsoft's software project architecture book)
Class Program
{
Static void main (string [] ARGs)
{
Console. writeline ("Please input four number :");
String year = console. Readline ();
Console. writeline ("Please input a number between 1 and 12 :");
String month = console. Readline ();
Console. writeline ("Please input a munber between 1 and 31 :");
String day = console. Readline ();
Console. writeline (Year. getdate ("Nian"). getdate (month + "Yue"). getdate (day + "Ri "));
Console. Readline ();
}
}
}
The output is as follows (the VM environment does not support Chinese characters ):
Please input four number:
2007
Please input a number between 1 and 12:
6
Please input a munber between 1 and 31:
14
2007nian6yue14ri
The last thing to emphasize is the call of the extension method. You can refer to the above Code to experience its flexibility.