C # 2.0 Specification (Five)

zhaozj2021-02-16  44

Type three

This article is a Microsoft's technical article for translation. For reference for learning C #, please do not use for commercial purposes. http://msdn.microsoft.com/vcsharp/team/language/default.aspx

20.6 generic method

The generic method is a method related to a particular type. In addition to conventional parameters, the generic method also names a set of type parameters that need to be provided when using methods. The generic method can be declared in class, structural or interface declaration, and they themselves can be generic or non-flourishing. If a generic method is declared in a generic type declaration, the method can reference the type parameters of the method and the type parameters that contain the declaration.

Class-Member-Declaration: (class member statement :)

...

Generic-method-declaration (generic method statement)

Struct-Member-Declaration: (Structure member statement :)

...

Generic-method-declaration (generic method statement)

Interface-Member-Declaration: (Interface member statement :)

...

Interface-generic-method-declaration (interface generic method declaration)

The declaration of generic methods can be implemented by placing a list of type parameters after the name of the method.

Generic-method-declaration: (generic method declaration :)

Generic-Method-Header Method-Body (generic method head method)

Generic-method-header: (generic method head :)

Attributes Opt Method-Modifiers Opt Return-Type Member-Name Type-Parameter-List (Formal-Parameter-List Opt) TYPE-Parameter-Constraints-Clause Opt

(Feature Optional Method Memor) Optional Return Type Member Name Type Parameter List (Formal Parameter List Optional) Type Parameter Binding Optional)

Interface-generic-method-declaration: (Interface generic method statement :)

Attributes Opt New Opt Return-Type Identifier Type-Parameter-List

(Formal-parameter-list opt) Type-Parameter-Constraints-Clauses Opt;

(Feature Optional NEW Optional Return Type Identifier Type Parameter List (Original Parameter List Optional) Type Parameter Binding Optional;

Type Parameter List and Type Parametric Constraints Statements with generic type declarations have the same syntax and functionality. The type parameter scope declared by the type parameter list runs through the entire generic method, which can be used to form a return value, a method, and a type parameter constraint statement, but does not include characteristics.

The name of the type parameters of the method cannot be the same as the common parameters of the same method.

The following example finds the first element in the array, exists if the given TEST commission is met. The generic commission is described in §20.4.

Public Delegate Bool Test (T Item);

Public Class Finder

{

Public Static T Find (T [] Items, Test Test) {

Foreach (T Item in Items)

{

IF (item (item) Return Item;

}

Throw New InvalidOperationException ("Item Not Found");

}

}

The generic method cannot be declared as extern. All other modes are effective in generic methods.

20.6.1 generic method signature

For the purpose of comparing the signature, any type of parameter constraint will be ignored, just like the name of the type parameter, but the number of type parameters is also corresponding, like the type parameter from left to right element position. The following example shows the method of signature under this rule.

Class a {}

Class B {}

Interface ix

{

T f1 (t [] a, int i); // error, because the return type and type parameter name is irrelevant,

Void F1 (u [] a, int i); // These two declarations have the same signature

Void F2 ; // ok, the number of type parameters is part of the signature

Void F2 (INT X);

Void F3 (T T) where t: a // error, constraints are not signed by the signature

Void F3 (T T) Where t: B:

}

The overload of the generic method has been further constrained by a rule that is similar to the reloading of the generic type declaration (§20.1.8). Two generic methods that use the same name and the same number of types of parameters cannot have the parameter type of the list of closed types, and two methods are generated in the same order in the same order, which is applied to the same order. Two methods are up. Since this rule constraint will not be considered. E.g

Class x

{

Void f (t t, u u) {...} // error, x .f generates two methods with the same signature

Void f (u u, t t) {...} //

}

20.6.2 Virtual generic method

The generic method can use Abstract, Virtual and Override modifiers. When the matching method is rewritten and the interface implementation, the signature matching the rules in §20.6.1 is used. When the generic method rewrites a generic method declared in the base class, or the method in which the matrix is ​​enabled, the constraint given for each type parameter must be the same in the two declarations, where the method type parameter will be The original position is identified from left to right.

Abstract Class Base

{

Public Abstract T f (t t, u u);

Public Abstract T g (t t) Where t: iComparable;

}

Class Derived: Base

{

Public override x f (x x, y y) {...} // ok

Public Override T g (t t) {...} // error

}

F'm rewriting is correct because the type parameter name is allowed. G 's rewrite is wrong because a given type parameter constraint (not constrained here) does not match the rewritten method.

20.6.3 Calling generics

Pan-type method calls can explicitly specify the type real parameter list, or omit the type real parameter list, depending on the type inference to determine the type of arguments. The exact compilation of the method call, including generic method calls, described in §20.9.5. When the generic method does not use the type parameter list, the type inference will be performed in §20.6.4.

The following example shows how the overload decision is happening after the type inferior and type real parameter replacement parameters.

Class test

{

Static void f (int x, t y)

{

Console.writeline ("one");

}

Static Void F (T x, long y)

{

Console.Writeline ("Two");

}

Static void

Main

()

{

F (5,324); // ok, print "one"

F (5,324); // OK, print "TWO"

F (5,324); // error, blurry

F (5,324); // ok, print "one"

F (5, 324L); // Error, blurry

}

}

20.6.4 Type Real Parts Inference

When you do not specify the type of primary parameters to call the generic method, the Type Inference process will attempt to call the invasive type. The presence of type inference allows to call generic methods, using more convenient syntax, and can avoid programmers to specify redundant type information. For example, given method declaration

Class Util

{

Static random rand = new random ();

Static Public T Choose (T First, T SECOND)

{

Return (Rand.Next (2) == 0)? First: Second

}

}

Non-explicit specifying types of collapsible parameters are also possible.

INT i = util.choose (5, 123); // Call Choose

String s = util.choose ("foo", "bar"); // Call Choose

By type inference, type untrusted int and string will be determined by the physical parameters of the method.

Type Introduction As part of the method call (§20.9.5) compiles, and occurs before the calling of the reload decision. When a specific method group is specified in a method call, the type untrustworthy is not specified as part of the method call, and the type inference will be applied to each generic method in the method group. If the type of inferred is successful, the inferred type is used to determine the real-ended type type of subsequent overload decision. If the overload decision selects the generic method to be called, the inferred type, the actual type of the invited, will be used as the actual type of the call. This method will not participate in the overload decision if the specific method type is inferred. Type Inference Failure It will not generate compile time errors. However, when the overload decision cannot find the applicable method, it will result in compile time errors.

If the number of reference number provided is different from the number of methods of the method, the inference will fail immediately. Otherwise, the type inference will occur independently in each formal argument provided to the method. Assume that the type of the argument is a, the corresponding parameter is the type P. Type Inference will be associated with type A and P in steps below.

If any of the following is established, you will not be able to infer anything from the first parameters (but type inference is successful)

- Any type parameters of P and methods do not close [1]

- Participation is NULL characters

- Participation is an anonymous method

- Real parametric is a method group

If P is an array type, A is an array type of the rank, then replaces A and P accordingly, and repeats this step.

If P is an array type, A is a different type of array, then the type of generic method has failed.

If P is the type parameters of the method, then the type of the type of argument is successful, and A is the type of inferred by that type.

Otherwise, P must be a structure type. If each method type parameter MX appears in P, you can determine a type Tx, replace each MX using each TX, which will generate a type, which can be used by the standard implicit conversion Conversion, then the type of referred to this inference is successful, for each MX, TX is the type of inferred. Method Type Parameters Constraint (if any), because the reason why the type is inferred will be ignored. If there is no TX exists or more than one TX for a given MX, the type of generic method will fail (more than one TX exists only, P is a generic interface type, and A A plurality of constructed versions of the interface are implemented. If all methods are dealt with by the previous algorithm, all inferences from the conference will be aggregated. This set of inferves must have the following properties.

Each type parameter of the method must have a type of arguments that are inferred. In short, this set of inferves must be complete (Complete);

If the type parameters are more than once, all inferences for that type parameters must infer the same type of arguments. In short, this set of interfaces must be consistent (consistent).

If you can find a set of completely consistent inferred types, the type inference can be said to be successful for a given generic method and a real-name list.

If the generic method uses the parameter array (10.5.1.4) declaration, the type inference will be performed in its usual manner. If the type is inference, the resulting method is available, then the method will be feasible to overload decisions in its usual form. Otherwise, the type inference will be performed for the extension form of the method (§7.4.2.1).

[1] is that P is not one of the types of type parameters of the method.

转载请注明原文地址:https://www.9cbs.com/read-25899.html

New Post(0)