The new features introduced in C #. NET Framewoke 3.0 provide extensions. NET class method , can increase the aesthetics of the Code!
There are several requirements for writing an extension method:
The class in which the extension method resides must be global, not an inner nested class, and the class of the extension method must be a static class .
The extension method must be a static method .
The data type of the first parameter of the extension method must be the type to be extended and the This keyword is used.
Extension method Definition:
public static class cexlongmethed{ //No parameter no return value public static void Print (this long L) { Console.WriteLine ( l); } The parameter has a return value of public static long Add (this long l, long L2) { return (L + L2);} }
Call:
Long lval = 1;lval.print (); Console.WriteLine (Lval.add (2));
C # Extension Methods extension method