LINQ Learning Series ----- 1.3 extension method, linq ----- 1.3

Source: Internet
Author: User

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

Related Article

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.