This article is a guide to: extension methods are defined as static methods, but they are invoked through the instance method syntax. Their first argument specifies which type the method acts on, and the parameter is prefixed with the This modifier. The extension method cannot, of course, break the concept of object-oriented encapsulation, so it can only access the public members of the extended class.
Extension methods enable you to "add" methods to an existing type without creating a new derived type, recompiling, or otherwise modifying the original type. An extension method is a special static method, but it can be called just like an instance method on an extended type.
C # extension method The first parameter specifies which type the method acts on, and the parameter is prefixed with the This modifier.
C # This extension method instance
Example 1, add an Add method to the string type, the function of which is to add a letter A
C # codeCopy
//Must be a static class to add an extension method StaticClassProgram{Static voidMain (String[] args){StringStr= "Quzijing";//Notice that the extension method is called, and the object must be used to invoke StringNewstr=Str. ADD (); Console.WriteLine (NEWSTR); Console.readkey (); } //Declaring extension methods//The extension method must be static, add has three parameters//This must have, string represents the type I want to extend, Stringname represents the object name//Three parameters this and the type of extension are necessary, the object name can be arbitrarily taken if you need to pass parameters,// add one more variable to public static string Add (this string Stringname) {return stringname< Span style= "color: #000000;" >+ "a" ; }}
Example 2, adding an extension method to the custom type, and adding a passed parameter
(1), declare a student class, it contains two methods Stuinfo,getstuinfo
C # codeCopy
Public ClassStudent{Public StringStuinfo (){Return "Basic information for Students"; } Public StringGetstuinfo (StringStuname,StringStunum){return< Span style= "color: #000000;" > string " Student information: \\n" Span style= "color: #000000;" > + name: {0} \\n " + number: {1}" , Stuname, stunum); }}
(2), declare a static class named Extensionstudentinfo, note must be static
The function of this class is to include some methods that we want to extend, where we declare two extension methods of the student type, and the student type is our custom type.
C # codeCopy
Public Static ClassExtensionstudentinfo{//Declaring extension methods//The method to be extended must be a static method, and add has three parameters//This must have, string represents the type I want to extend, Stringname represents the object name//Three parameters this and the type of extension are necessary, the object name can be arbitrarily taken if you need to pass parameters, and then add a variable can be Public Static StringExtensionstuinfo (ThisStudent stuname){ReturnStuname.stuinfo (); } //Declaring extension methods//The method to be extended must be a static method, and add has three parameters//This must have, string represents the type I want to extend, Stringname represents the object name//Three parameters this and the type of extension are necessary, the object name can be arbitrarily taken if you need to pass parameters, where we added two string type parameters Public static string extensiongetstuinfo (this Student Student, string stuname, String stunum) { return student.getstuinfo (Stuname, Stunum)+"\\n Read complete ";} }< /c1>
(3), using the extension method of the custom class, note that the extension method must be called with the object
C # codeCopy
Static voidMain (String[] args){Student newstudent= NewStudent ();//To invoke our extension method using an object string stuinfo = newstudent. Extensionstuinfo (); Console.WriteLine (Stuinfo); // to invoke our extension method using an object string stuinformation = newstudent. Extensiongetstuinfo ("quzijing", "20081766"); Console.WriteLine (stuinformation); Console.readkey (); }
Example 3, extending a validation message class for string
(1), extension method
C # codeCopy
UsingSystem;UsingSystem.Collections.Generic;UsingSystem.Linq;UsingSystem.Text;UsingSystem.Text.RegularExpressions;//To declare the extension method: The class must be static, the method is static,//The first parameter is the extended object, preceded by this.//Extension methods must be used to ensure that the extension method class is already in the current code using theNamespaceExtension methods{//The extension method must be static Public Static ClassStringhelper{//The extension method must be static, and the first parameter must be prefixed with this Public Static boolIsemail (This String_input){ReturnRegex.IsMatch (_input,@"^\\[email protected]\\w+\\.\\w+$"); } //extension method with multiple parameters//Adds the specified character before and after the original string public static string Quot ( Span style= "color: #0000ff;" >this string _input, Span style= "color: #0000ff;" >string _quot) {return _quot + _input + _quot;} }} /span>
(2), how to use
C # codeCopy
UsingSystem;UsingSystem.Collections.Generic;UsingSystem.Linq;UsingSystem.Text;NamespaceExtension methods{ClassProgram{Static voidMain (String[] args){String _myemail = "[email protected]" // Here you can use the extension method of the String class Isemail the Console.WriteLine (_myemail.isemail ()); // Call extension method for receiving parameters Console.WriteLine (_myemail.quot ( "! "}}
C # This extension method