C # Extensions

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.