C #3.0 specification (iii) extended functions

Source: Internet
Author: User

26.1 extension methods extension Functions

Extension methods are static methods that can be invoked using instance method syntax. In effect, extension methods make it possible to extend existing types and constructed types with additional methods.

An extension function is a static function that can be called using the instance function syntax. Extended functions can effectively extend existing types and use extra function construction types.

NoteNote:

Extension methods are less discoverable and more limited in functionality than instance methods. For those reasons, it is recommended that extension methods be used sparingly and only in situations where instance methods are not feasible or possible.

Extended functions are less used and more limited in functional functions than in Instance functions. For these reasons, we recommend that you use an extended function without any inconvenience and use it only when the instance function does not work or is not available.

Extension members of other kinds, such as properties, events, and operators, are being considered but are currently not supported.

Extension members of other types, such as attributes, events, and operators, are under consideration, but are not yet supported by the compiler.

26.1.1 declaring extension methods declare extension Functions

Extension methods are declared by specifying the keyword this as a modifier on the first parameter of the methods. extension methods can only be declared in static classes. the following is an example of a static class that declares two extension methods:

The Extension function uses the specific keyword "this" as the modifier to be declared in the first parameter of the function. An extension function can only be declared in a static class. The following is an example of a static class that declares two extension functions:

Namespace Acme. Utilities
{
Public static class Extensions
{
Public static int ToInt32 (this string s ){
Return Int32.Parse (s );
}

Public static T [] Slice <T> (this T [] source, int index, int count ){
If (index <0 | count <0 | source. Length-index <count)
Throw new ArgumentException ();
T [] result = new T [count];
Array. Copy (source, index, result, 0, count );
Return result;
}
}
}

Extension methods have all the capabilities of regular static methods. In addition, once imported, extension methods can be invoked using instance method syntax.

Extended functions have all the capabilities of conventional static functions. Additionally, Once imported, extended functions can be called using the syntax of Instance functions.

26.1.2 importing extension methods import extension Functions

Extension methods are imported through using-namespace-directives (§ 9. 3.2 ). in addition to importing the types contained in a namespace, a using-namespace-directive imports all extension methods in all static classes in the namespace. in effect, imported extension methods appear as additional methods on the types that are given by their first parameter and have lower precedence than regular instance methods. for example, when the Acme. utilities namespace from the example abve is imported with the using-namespace-directive

Extended functions are imported using-namespace-directives (§ 9. 3.2. In addition, the type of the namespace is imported. using-namespace-directive imports all the static classes of the extended functions in the namespace. The imported extended functions are effectively used as additional functions of some types, which are given by their first keyword and a lower priority than the conventional Instance functions. For example, after Acme. Utilities namespace is imported using-namespace-directive at the beginning of the following example,

Using Acme. Utilities;

It becomes possible to invoke the extension methods in the static class Extensions using instance method syntax:

This makes it possible to call the extension functions of the static Extensions class through the syntax of the Instance function:

String s = "1234 ";
Int I = s. ToInt32 (); // Same as Extensions. ToInt32 (s)

Int [] digits = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9 };
Int [] a = digits. Slice (4, 3); // Same as Extensions. Slice (digits, 4, 3)

26.1.3 call of extension method invocations Extension function

The detailed rules for extension method invocation are described in the following. In a method invocation (§ 7. 5.5.1) of one of the forms

Detailed Rules for calling extended functions are described below. In any form of function call (§ 7. 5.5.1)

Expr. identifier ()

Expr. identifier (args)

Expr. identifier <typeargs> ()

Expr. identifier <typeargs> (args)

If the normal processing of the invocation finds no applicable instance methods (specifically, if the set of candidate methods for the invocation is empty ), an attempt is made to process the construct as an extension method invocation. the method invocation is first rewritten to one of the following, respectively:

If no applicable instance function is found during a normal call (especially if the candidate function for this call is empty), the compiler will try to call the constructor as an extension function. Function calls rewrite one of the following:

Identifier (expr)

Identifier (expr, argS)

Identifier <typeargs> (expr)

Identifier <typeargs> (expr, argS)

The rewritten form is then processed as a static method invocation, except t for the way in which identifier is resolved: Starting with the closest enclosing namespace declaration, continuing with each enclosing namespace declaration, and ending with the containing compilation unit, successive attempts are made to process the rewritten method invocation with a method group consisting of all accessible extension methods with the name given by identifier imported by the namespace declaration's using-namespace-directives. the first method group that yields a non-empty set of candidate methods is the one chosen for the rewritten method invocation. if all attempts yield empty sets of candidate methods, a compile-time error occurs.

The format of rewriting is treated as static function call processing, unless identifier is parsed: start to use the most recent namespace declaration, then use the nearly namespace declaration, and finally contain the compilation unit, successive attempts are used to process rewrite function calls. This is implemented by a function group consisting of all the extended functions that are allowed to access the database. The name of this function is given by identifier, identifier import is implemented by the namespace declared using-namespace-directives. The first function group generates a set of non-empty candidate functions, one of which will be selected to call the rewrite function. If all the attempts generate null candidate functions, this will lead to a compilation error.

The preceeding rules mean that instance methods take precedence over extension methods, and extension methods imported in inner namespace declarations take precedence over extension methods imported in outer namespace declarations. For example:

The preceding rules mean that the instance function takes precedence over the extension function, and the extension function in the namespace takes precedence over the extension function imported through the namespace. For example:

Using N1;

Namespace N1
{
Public static class E
{
Public static void F (this object obj, int I ){}

Public static void F (this object obj, string s ){}
}
}

Class {}

Class B
{
Public void F (int I ){}
}

Class C
{
Public void F (Object OBJ ){}
}

Class X
{
Static void test (A a, B, c ){
A. F (1); // e.f (object, INT)
A. F ("hello"); // e.f (object, string)

B. F (1); // B. F (INT)
B. F ("hello"); // e.f (object, string)

C. F (1); // C. F (object)
C. f ("hello"); // C. F (object)
}
}

In the example, B's method takes precedence over the first extension method, and C's method takes precedence over both extension methods.

In this example, B's function takes precedence over the previous extension function, and C's function takes precedence over all extension functions.

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.