. Net extension Methods
Profile:
I believe a lot of people like me, are consciously using the "extension method" this technology, but basically do not have to manually write an extension method to develop, here I do the "extension method" Summary and simple small application.
Body:
1. Conceptual stuff:
first of all, the extension method is essentially a static method , and after compiling, it is actually implemented by invoking static methods with static classes. The instance method can be overloaded, but the instance method is called first. and, extension methods defined on the parent class can be used in subclasses.
2. Actual code:
<summary>
The first argument of this method must begin with this and specify which type the method is extended from
</summary>
public static Class Myexpandclass
{
public static string Myexpandfunc (this int inputparam)
{
Return "Inputparam:" + inputparam.tostring ();
}
}
static void Main (string[] args)
{
int intput = 19;
String Returnvalue=intput.myexpandfunc ();
Console.WriteLine (returnvalue);
}
results :inputparam:19
In this way, an extension method Myexpandfunc() is completed under the INT type.
3. Extension content:
As you can see, the return value is string, extended on the int type, then you can imagine that depending on the return value, but also write a bunch of different extension methods, according to different extension types and write a bunch of different extension methods, it is obviously unreasonable. But Microsoft offers a good solution to the demo: As can be seen, a set of extension methods in the Ienumerable<t> generic interface should now know the solution to the problem raised above.
Personal Summary:. NET extension methods