Http://blog.sina.com.cn/s/blog_a5193ed401016mvb.html
An extension method is a special static method, but it can be called like an instance method of an extension type.
Extension methods are defined as static methods, but they are called through the instance method syntax. Their first parameter specifies the type of the method to act on, and the parameter usesThisThe modifier is the prefix.
The extension method requirements are as follows:
1. The first parameter is the type to be extended or to be operated. This is called "extended type"
2. to specify the extension method, add the this modifier before the extended type name.
3. Access the method as an extension method. Use the using command to import the extension type namespace, or make the extension type and calling code in the same namespace.
- // The extension method must be defined in a non-generic static class.
- Public static class qzwtest
- {
- // Extension Method
- Public static string [] qzw (this string Str)
- {
- Return Str. Split (New char [] {'', ','});
- }
- }
- Class Program
- {
- Static int main ()
- {
- String teststr = "Qian zhuowen is Miao, Xian Jing ";
- // Call the Extension Method
- String [] testarray = teststr. qzw ();
- Foreach (string s in testarray)
- {
- Console. writeline (s );
- }
- Console. readkey ();
- Return 0;
- }
- }