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. This is the description of MSDN. I see a lot of blogs that begin with the above sentences. So that's how I start.
Originally thought about a blog review of the following generics, will be specific fuzzy, this should be reflective, will be vague embodiment, but see the reflection of things a lot, one night I also summed up, but also to leave some time to fly it. So think about it and then summarize the expansion of it.
First, why should there be an extension method?
The beginning also says that you do not have to create a new derived type, recompile, or otherwise modify the original type to add methods to an existing class or interface. For example, there are often helper tools classes, such as string and time, before they are expanded. With the extension we can directly extend the string class or the time class, so that you do not have to instantiate the helper class to directly handle.
Ii. What are the characteristics of the extension method?
An extension method is a static method, which is part of a class, but is not actually placed in the source code of the class.
The class where the extension method resides must also be declared as static
C # supports only extension methods, does not support extended properties, extended events, and so on.
The first parameter of the extension method is the type to extend, followed by the This keyword, and the parameter after this does not belong to the parameter of the method
In an extension method, you can access all public methods and properties of the extension type.
The extension method extends from which type, it must be a variable of this type to use, other types cannot be used
If the extension method and the instance method have the same signature, the instance method is invoked first
Extended from a method on a parent class, you can use the object of the quilt class directly
Extend the method on the interface, which can be used directly by the object that implements the class
The extension method is eventually compiled by the compiler: Static class. static method ()
Third, demo
Above a few basic summary of the summary of the extension is finished, below to do a demo to illustrate.
1. Define the IAnimal interface declaration void Eat ();
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace extensionmethod{ publicinterface ianimal { void Eat (); }}
2. Define the person class to implement the interface IAnimal implement void Eat ();
usingSystem;usingSystem.Collections.Generic;usingSystem.Linq;usingSystem.Text;usingSystem.Threading.Tasks;namespaceextensionmethod{ Public classPerson:ianimal { Public voidEat () {Console.WriteLine ("Person Eat"); } }}
3. Define extension methods Extensionmethod
usingSystem;usingSystem.Collections.Generic;usingSystem.Linq;usingSystem.Text;usingSystem.Threading.Tasks;namespaceextensionmethod{ Public Static classExtensionmethod { Public Static voidEat ( Thisianimal IAnimal) {Console.WriteLine ("ianimalextension Eat"); } Public Static voidSleep ( Thisianimal IAnimal) {Console.WriteLine ("ianimalextension Sleep"); } Public Static voidEat ( ThisPerson person ) {Console.WriteLine ("personextension Eat"); } Public Static voidSleep ( ThisPerson person ) {Console.WriteLine ("personextension Sleep"); } }}
The above defines 4 extension methods in the Extensionmethod class, two are extensions to the interface IAnimal, and two are extensions to the person class.
4. Instantiation of the test
usingSystem;usingSystem.Collections.Generic;usingSystem.Linq;usingSystem.Text;usingSystem.Threading.Tasks;namespaceextensionmethod{classProgram {Static voidMain (string[] args) {IAnimal P=NewPerson (); P.eat (); P.sleep (); Console.WriteLine ("----------------This is the bottom line------------------------"); Person P1=NewPerson (); P1. Eat (); P1. Sleep (); Console.WriteLine ("----------------This is the bottom line------------------------"); Extensionmethod.eat (P); Extensionmethod.sleep (P); Console.WriteLine ("----------------This is the bottom line------------------------"); Extensionmethod.eat (p1); Extensionmethod.sleep (p1); Console.ReadLine (); } }}
The above Mian method first instantiates a person object, assigns a variable to the IAnimal type, calls the Eat (), and the Sleep () method. It then instantiates a person object, this time assigning to a variable of type person.
Here's a look at the results of the operation is not unexpected:
P and P1 We can compare the analysis, for the Eat () method is output "person Eat", if the extension method and the instance method has the same signature, then the first invocation of the instance method, this sentence just can explain why. But for the sleep () method, we can see that the extension method of the interface is called with a variable of type ianimal, and the extension method of the person type is called with a variable of type person. The extension method extends from which type, it must be a variable of this type to use, other types can not be used, and this sentence although a bit different, but it is quite consistent. I understand this: for a method of the same name, the instance method overrides the method, and its own extension method overrides the parent class method. It may also be because the subclass overrides the extension method of the parent class.
We can annotate the extension method of person pairs and see the results of the run.
usingSystem;usingSystem.Collections.Generic;usingSystem.Linq;usingSystem.Text;usingSystem.Threading.Tasks;namespaceextensionmethod{ Public Static classExtensionmethod { Public Static voidEat ( Thisianimal IAnimal) {Console.WriteLine ("ianimalextension Eat"); } Public Static voidSleep ( Thisianimal IAnimal) {Console.WriteLine ("ianimalextension Sleep"); } //Public static void Eat//{ //Console.WriteLine ("Personextension Eat"); //} //Public static void Sleep//{ //Console.WriteLine ("Personextension Sleep"); //} }}
From the above results can be seen, extend the method on the interface, can be implemented directly by the object of the class, in fact, extended from the parent class method, you can use the object of the quilt class directly and interface similar
Extensions of C # syntax