This is a really cool new feature. You can specify the parameter type for the method during compilation. For example, if you want to add a method, the parameter type is string. Note that the "this" keyword must be added before the parameter. Let's try a demo:
Code
Using system;
Using system. Collections. Generic;
Using system. LINQ;
Using system. Web;
Using system. Web. UI;
Using system. Web. UI. webcontrols;
Namespace webapplication1
{
Public partial class extendmethod: system. Web. UI. Page
{
Protected void page_load (Object sender, eventargs E)
{
String name = "Qiujuan ";
String text = Name. sayhello ();
Response. Write (text );
}
}
// The extension method must be defined in the top-level static class and cannot be defined in the nested class.
Public static class extendclass
{
Public static string sayhello (this string username)
{
Return string. Format ("Hello, {0}", username );
}
}
}