extension methods in C #

Source: Internet
Author: User

extension methods useIn a real project, you typically define a static class that contains common methods of action, such as string processing, determining whether a string is the correct phone number or email, and so on, defining these methods as static methods, using the static class. static method (related parameter) so that you don't have to instantiate the class. , reduce the amount of code, and then the extension method can make use of less code. The concept of extension methods is introduced in c#3.0, using: Precede the first parameter of the extension method with the This keyword. The following illustrates the simple use of extension methods:
Static  classCommonhelper { Public Static stringTransferstrtoupper ( This stringstr) {          returnStr.      ToUpper (); }    }  classProgram {Static voidMain (string[] args) {            stringstr ="ABCDEFG"; stringstr1=commonhelper.transferstrtoupper (str); stringStr2=Str.            Transferstrtoupper (); Console.WriteLine ("Str1={0},str2={1}", STR1,STR2);        Console.readkey (); }    }

Variable STR1,STR2 can be successfully converted to uppercase. On the surface, the variable str has a Transferstrtoupper method, but in essence it is still a static method.

extension Method NatureThe essence of the extension method is not just to change the way the code is written, but to demonstrate how to create and use an extension method. The introduction of extension methods can simulate the creation of instance methods for different class classes. Suppose there is a scenario where the actual project has a string processing class such as Saylove, but the class does not allow to modify its source code and is a sealed class (not allowed to be inherited), but it will use some methods in the class, such as method Sayloveyou, Of course you can set another class Sayloveextend, in this Class A method Expresslove class Saylove instance as a parameter, so you can use the class Saylove internal methods, can solve the problem. But if the new method Expresslove can be raised in the instance of class Saylove instead of creating another instance of the class sayloveextend that is acting on it, it looks better, as if the new method is a method of class Saylove internal. The following is a description of the description of this paragraph of the program:
Sealed classSaylove { Public stringSayloveyou (stringpersonname) {            returnPersonName +", I love you."; }    }Static classSayloveextend { Public Static stringExpresslove ( ThisSaylove SL,stringgirlfirend) {            returnSl. Sayloveyou (girlfirend) +"Marry me."; }    }classProgram {Static voidMain (string[] args) {            stringstr ="Lily"; Saylove SL=NewSaylove (); stringstr1=SL.            Expresslove (str);            Console.WriteLine (STR1);        Console.readkey (); }    }

Program output:

extension methods in C #

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.