MSDN defines extension methods as follows: "extension methods are defined as static methods, but they are invoked through the instance method syntax. Their first argument specifies which type the method acts on, and the parameter is prefixed with the This modifier. ”
Create a new class named EString.cs in the class library
usingSystem;usingSystem.Collections.Generic;usingSystem.Linq;usingSystem.Text;namespacew.common{ Public Static classestring {/// <summary> ///Convert a string to an int/// </summary> /// <param name= "T" ></param> /// <returns>returns 0 when conversion fails</returns> Public Static intToInt ( This stringt) {intID; int. TryParse (T, outID);//here, when the conversion fails, the ID returned is 0. returnID; } }}
There is a toint static method in Estring, he receives a parameter of this, the type string,this string must be at the top of the method parameter, it conforms to the definition criteria, so toint is an extension method.
Referring to the namespace of the extension method can be called directly
/// <summary> /// using extension methods /// </summary> Private void intexttest () { string""; int i = s.toint (); }
Blog Original: http://www.cnblogs.com/suger/archive/2012/05/13/2498248.html
What is an extension method