Extension method parsing for C #

Source: Internet
Author: User

I. Overview of extension methods:

The "extension method" is introduced in c#3.0, which has the advantages of static methods, and the readability of the code that calls them has been improved. When you use an extension method, you can call a static method just as you would call an instance method.

1. Basic principles of extension methods:

(1). C # supports only extension methods and does not support extended attributes, extended events, extension operators, and so on.

(2). The extension method (the method before the first parameter is this) must be declared in a non-generic static class, the extension method must have one parameter, and only the first parameter uses the this tag.

(3). When the C # compiler looks for extension methods in static classes, it requires that the static classes themselves have a file scope.

(4). C # compilation requires an "import" extension method. (Static methods can be arbitrarily named, and the C # compiler takes time to look up when looking for methods, needs to examine all static classes in the file scope, and scans all of their static methods to find a match)

(5). Multiple static classes can define the same extension method.

(6). When you extend a type with an extension method, the derived type is also extended.

2. Extension method declaration:

(1). Must be in a non-nested, non-generic type static class (so it must be a static method)

(2). There is at least one parameter.

(3). The first parameter must be prefixed with the This keyword.

(4). The first parameter cannot have any other modifiers (such as ref or out).

(5). The type of the first parameter cannot be a pointer type.

Two. Extension method principle Analysis:

The extension method is a unique method of C # that uses the ExtensionAttribute attribute in the extension method.

C # Once the first parameter of a static method is marked with the This keyword, the compiler internally applies a custom attribute to the method, which is persisted in the metadata of the resulting file, which is stored in System.core DLL assemblies.

Any assembly contains at least one static class that conforms to the above characteristics, and its metadata will also apply the attribute. If the code is using a nonexistent instance method, the compiler can quickly scan all referenced assemblies, determine which ones contain extension methods, and then, in this assembly, scan the static classes that contain the extension methods.

If two classes in the same namespace contain methods with the same extension type, there is no way to use only the extension methods in one of the classes. In order to use a type with a simple name of type (without naming the control prefix), you can import all the namespaces in that type, but in doing so, you have no way to prevent the extension methods in that namespace from being imported.

Three.. NET3.5 extension methods Enumerable and queryable:

In the framework, the biggest use of extension methods is for LINQ services, which provide an auxiliary extension method, located in the enumerable and Queryable classes under the System.Linq namespace. Enumerable most extensions are ienumerable<t>,queryable large majority of the extensions are iqueryable<t>.

Common methods in the 1.Enumerable class:

(1). Range (): One parameter is the starting number, and the other is the number of results to be generated

Extension method parsing for C #

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.