The extension method enables you to add a method to an existing type without creating a new derived type, recompiling, or otherwise modifying the original type. "This is what MSDN says, which is that you can add one or more methods based on these types of string,int,datarow,datatable and so on, without having to modify or compile the code for the type itself. ///extension Methods///1.0 The first parameter of the extension method represents the type of extension to be preceded by this///2.0 The extension method itself is a static method///3.0 extension methods must be placed in a static class///4.0 extension methods can form method overloads with instance methods, but the call priority of the Power method is higher than the extension method///5.0 The extension method defined in the parent class can be used by the quilt classThe following are the extension methods: Public Static stringFmtdatetime ( ThisDateTime Now) {returnNow. ToString ("YYYY-MM-DD");}/// <summary>///of the extended/// </summary>/// <param name= "str" ></param>/// <returns></returns> Public Static stringToLower ( This stringStrstrings) {returnStr. ToLower () +"from the extended"+s;} Public Static stringGetpigname ( Thispig Pig) {returnPig. Name +"from the extended";} Public Static voidSay ( Thispig Pig) {Console.WriteLine (pig. ToString ());} So, you can use extension methods like this:#region7.0 extension methodsStatic voidC07exmethod () {DateTime time=DateTime.Now;////Call a static method in a class//Console.WriteLine (Extmethodhelper.fmtdatetime (time));//extension method, Syntax sugar (for vs compiler, belongs to compiler syntax, not C # syntax)//essentially through the Extmethodhelper.fmtdatetime () methodConsole.WriteLine (time. Fmtdatetime ());//2.0 The lowercase extension method of a string with the precedence of the instance method of the string itselfstringstr ="AB"; Console.WriteLine (str. ToLower ("Extended"));//3.0 extension method extension subclasses, as subclasses can useSmallpig SM =NewSmallpig () {Name ="piglets inherit from old pigs" }; Console.WriteLine (Sm. Getpigname ());}#endregion
C # Extensions