LINQ Learning Series ----- 1.3 extension method, linq ----- 1.3
This article continues with the source code of yesterday's Lambda expression. Yesterday I spoke about Lambda expressions. This article describes the extension methods. These two points are new features of Linq.
1. Introduction to extension methods
If you don't talk nonsense, go to the source code first:
GetMemoryCount is an extension method. The following information can be obtained from the annotation:
1. Add this keyword to the extension method.
2. The extension method and the included class must be static.
3. The contained class cannot be a generic class.
In-depth summary:
1. The extension method can receive any number of parameters, but the first parameter must be modified with this.
2. The extension method is based on an object. For example, if you suddenly want to add another method to this object, you can use the extension method. The first parameter is the parameter based on this object. (The cornerstone of the extension method is the object)
II. Smart sensing of extended Methods
When the code of the extension method is completed and there is no problem. When you call the extension method of this object, IDE will have smart awareness. See the arrow:
In, you can also see that the extension method icons are different, are marked with a blue drop.
3.. Chained statement of the Extension Method
As you can see, the benefit of the extension method is that the statement can be chained.
Thu. Conflict between the extension method and the normal method
First go to the source code:
As you can see, if no corresponding method is found in the object, the extension method will be executed (the object is all the base classes ). Execution result:
Source code:Linqlearning series -----1.3extended protocol (source code .rar