extension Methods of LINQ

Source: Internet
Author: User

Directory

Write in front

Series Articles

Extension methods

Summarize

Write in front

The previous article introduced the implicit type, the automatic attributes, the initializer, the related concepts of anonymous classes, and the way the compiler looked at the compilers to help us do those things. This article will describe the knowledge points of the extension method and how to define and how to use the relevant content.

Series Articles

A preliminary understanding of the lambda Expression of LINQ

The Lambda Advanced of LINQ

Implicit types of LINQ, automatic attributes, initializers, anonymous classes

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.

The most common extension method is the LINQ standard query operator, which adds query functionality to existing System.Collections.IEnumerable and system.collections.generic.ienumerable<t > type. To use standard query operators, first use the using SYSTEM.LINQ directive to place them in scope. Then, any type that implements the ienumerable<t> appears to have GroupBy, Average, and so on. When you type "dot" after an instance of the ienumerable<t> type, such as list<t> or Array, you can see these additional methods in IntelliSense statement completion.

First look at Ah, how the expansion method rose like

You can find that the class where the extension method resides must be a static class, the method must be a static method, and the first parameter of the method must begin with the this modifier.

How do I customize extension methods?

1. Define a static class to contain extension methods. The class must be visible to client code.

2. Implement the extension method as a static method and make it at least as visible as the containing class.

3. The first parameter of the method specifies the type that the method operates on, and the parameter must begin with the this modifier.

4. In the calling code, add a using directive to specify the namespace that contains the extension method class.

5. Call the extension method in the same way as an instance method on the invocation type.

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.

An example

Extend a method for string: Returns the number of bytes of a string. Add a class like this to your project Stringextension

1 namespaceWolfy.LinqDemo.Extensions2 {3     /// <summary>4     ///String type extension method5     ///Static Class6     /// </summary>7      Public Static classstringextension8     {9         /// <summary>Ten         ///Gets the number of bytes in the string One         /// </summary> A         /// <param name= "source" >string</param> -         /// <returns>byte length</returns> -          Public Static intGetbyteslength ( This stringsource) the         { -             byte[] bytes =Encoding.Default.GetBytes (source); -             returnbytes. Length; -         } +     } -}

In use, first introduce the namespace of the static class where your extension method resides, for example in this case, you need to introduce

1 // first introduce namespaces 2 using Wolfy.LinqDemo.Extensions;

Then you can use that as you define it as defined in the String class.

The code also finds such a problem, and the extension method provides a way to add behavior to the class without modifying the source code. For example here we cannot modify the source code of the string class, nor can we add a Getbyteslength method to the method of the string class, but by extending the method, as the client uses, it is like the method of the string class.

When the mouse is placed on the method name, it is also suggested that the method is an extension method,

Output results

Summarize

1, the extension method is a static method, where the class must be a static class, the first parameter of the method must begin with the this modifier.

2, the extension method when used, must be able to access.

3, in the use of the extension method should be introduced in the namespace. (Of course you can also directly use the namespace of the type you want to extend, such as a string namespace of system, you can use system as the Stringextension namespace, so you don't need to introduce namespaces when you use them.) But it is not recommended to do so! )

Reference articles

Msdn:http://msdn.microsoft.com/zh-cn/library/bb383977.aspx

Http://msdn.microsoft.com/zh-cn/library/bb311042.aspx

extension Methods of LINQ

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.