This article mainly introduces the C # extension method, combined with the example form analysis of C # extension method of the function, the use of methods and related considerations, the need for friends can refer to the following
This example describes the C # extension method. Share to everyone for your reference, as follows:
Extension methods
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. For client code written in C # and Visual Basic, there is no significant difference between calling extension methods and calling methods that are actually defined in the type.
If we had a requirement to convert the first character of a string to uppercase, the second character to the nth character to lowercase, the other unchanged, how do we do it?
Do not use extension methods:
Using system;using system.collections.generic;using system.linq;using system.text;using System.Threading.Tasks; namespace extramethod{ //abstract static Stringhelper class public static class Stringhelper { //abstract to capitalize the first character of a string, From the first to the Len lowercase, the other immutable method public static string topascal (String S,int len) { return s.substring (0, 1). ToUpper () + s.substring (1, Len). ToLower () + s.substring (len + 1); } } Class program { static void Main (string[] args) { string s1 = "ASDDADFGDFSF"; String s2 = "SBFSDFFSJG"; Console.WriteLine (Stringhelper.topascal (s1,3)); Console.WriteLine (stringhelper.topascal (S2, 5));}}}
To use the extension method:
Using system;using system.collections.generic;using system.linq;using system.text;using System.Threading.Tasks; Namespace extramethod{ class program { static void Main (string[] args) { string s1 = "ASDDADFGDFSF "; String s2 = "SBFSDFFSJG"; Console.WriteLine (S1. Topascal (3)); Console.WriteLine (S2. Topascal (5)); } } The extension class, as long as it is static, can be public static class Extraclass { //extension Method-Special static Method--Add a special method for string type topascal public static string Topascal (this string s, int len) { return s.substring (0, 1). ToUpper () + s.substring (1, Len). ToLower () + s.substring (len + 1);}}
By comparing the above two methods:
1. The code is more convenient to access static methods such as Topascal. Used as if the extension type does have the same instance method.
2. Extension methods do not change the code of the extended class without recompiling, modifying, deriving the extended class
Defining extension methods
1. Define a static class to contain extension methods.
2. The class must be visible to client code.
3. Implement the extension method as a static method and make it at least as visible as the containing class.
4. The first parameter of the method specifies the type that the method operates on, and the parameter must begin with the this modifier.
Note that the first parameter is not specified by the calling code because it represents the type of operator being applied, and the compiler already knows the type of the object. You only need to provide the arguments for these two parameters through N.
Precautions:
1. Extension methods must be defined in a static class
2. Extension methods have a lower priority than class methods with the same name
3. Extension methods are only valid within a specific namespace
4. Do not misuse the extension method unless necessary