C# 2.0 Specification (Generics 4)

黄舟
Release: 2017-01-03 10:35:08
Original
1398 people have browsed it

Connected to Generics 3

20.6 Generic methods

Generic methods are methods related to a specific type. In addition to regular parameters, a generic method also names a set of type parameters that need to be supplied when using the method. Generic methods can be declared in a class, struct, or interface declaration, and they themselves can be generic or non-generic. If a generic method is declared in a generic type declaration, the method body can reference the method's type parameters and contain the declared type parameters.

class-member-declaration:(类成员声明:)
…
generic-method-declaration (泛型方法声明)
struct-member-declaration:(结构成员声明:)
…
generic-method-declaration(泛型方法声明)
interface-member-declaration:(接口成员声明:)
…
interface-generic-method-declaration(接口泛型方法声明)
泛型方法的声明可通过在方法的名字之后放置类型参数列表而实现。
generic-method-declaration:(泛型方法声明:)
generic-method-header method-body(泛型方法头 方法体)
generic-method-header:(泛型方法头:)
Copy after login

attributes opt method-modifiers opt return-type member-name type-parameter-list(formal-parameter-list opt) type-parameter-constraints-clause opt
( Features optional method modifier optional return type member name type parameter list (formal parameter list optional) type parameter constraint statement optional)
interface-generic-method-declaration: (interface generic method declaration:)
attributes opt new opt return-type identifier type-parameter-list
(formal-parameter-list opt) type-parameter-constraints-clauses opt ;
(Attribute optional new optional return type identifier type parameter List (formal parameter list is optional) Type parameter constraint statement is optional;
Type parameter list and type parameter constraint statement have the same syntax and function as the generic type declaration. The type parameter scope declared by the type parameter list runs throughout. Generic method declaration, which can be used to form a constraint statement including return value, method body and type parameter, but does not include attributes.

The name of the type parameter of the method cannot be the same as the name of the regular parameter of the same method.

The following example finds the first element in an array that exists if it satisfies the given test delegate. Generic delegates are described in §20.4.

public delegate bool Test<T>(T item);
public class Finder
{
public static T Find<T>(T[] items , Test<T> test){
foreach(T item in items)
{
if(test(item)) return item;
}
throw new InvalidOperationException(“Item not found”);
}
}
Copy after login

Generic methods cannot. Declared as extern. All other modifiers are valid on generic methods.

##20.6.1 Generic method signatures

For signature comparison purposes, any type parameter. Constraints are ignored, as are the names of the type parameters, but the number of type parameters is also ignored, as is the element position of the type parameters from left to right. The following example shows the method signature affected by this rule. .



class A{}
class B {}
interface IX
{
T F1<T>(T[] a , int i); //错误,因为返回类型和类型参数名字无关紧要,
void F1<U>(U[] a ,int i); //这两个声明都有相同的签名
void F2<T><int x>; //OK,类型参数的数量是签名的一部分
void F2(int x);
void F3<T>(T t) where T: A // 错误,约束不在签名考虑之列
void F3<T>(T t) where T:B: 
}
Copy after login

Overloading of generic methods follows a similar rule to that governing method overloading in generic type declarations (§20.1.8), with two further constraints. A generic method declaration with the same name and the same number of type arguments cannot have a parameter type that encloses a list of type arguments, when they produce two methods with the same signature in the same order, being applied to both methods in the same order. When a method is used, the constraint will not be considered due to this rule. For example,


##

class X<T>{void F<U>(T t , U u){…} //错误,X<int>.F<int> 产生了具有相同签名的两个方法void F<U>(U u, T t){…} //}
Copy after login

20.6.2 Virtual generic method

The generic method can use abstract. virtual and override modifier declarations. When matching method overrides and interface implementations, signatures matching the rules described in §20.6.1 will be used. When a generic method overrides a generic method declared in a base class, or implements a method in a base interface, the constraints given for each type parameter must be the same in both declarations, where the method type parameters will be Original positions are identified from left to right.

abstract class Base


{
public abstract T F<T,U>(T t, U u);
public abstract T G<T>(T t) where T: IComparable;
}
class Derived:Base
{
public override X F<X,Y>(X x ,Y y){…} //OK
public override T G<T>(T t){…} //错误
}
Copy after login

The rewriting of F is correct because the type parameter names are allowed to be different. The override of G is wrong because the given type parameter constraints (there are no constraints here) do not match the overridden method.


20.6.3 Calling generic methods

泛型方法调用可以显式指定类型实参列表,或者省略类型实参列表,而依靠类型推断来确定类型实参。方法调用的确切编译时处理,包括泛型方法调用,在§20.9.5中进行了描述。当泛型方法不使用类型参数列表调用时,类型推断将按§20.6.4中所描述的进行。

下面的例子展示在类型推断和类型实参替代参数列表后,重载决策是如何发生的。

class Test
{
static void F<T>(int x , T y)
{
Console.WriteLine(“One”);
}
static void F<T>(T x , long y)
{
Console.WriteLine(“two”);
}
static void Main()
{
F<int>(5,324); //ok,打印“one”
F<byte>(5,324); //ok, 打印“two”
F<double>(5,324); //错误,模糊的
F(5,324); //ok,打印“one”
F(5,324L); //错误,模糊的 
}
}
Copy after login

20.6.4类型实参推断

当不指定类型实参而调用泛型方法时,类型推断(type inference)处理将试图为调用推断类型实参。类型推断的存在可以让调用泛型方法时,采用更方便的语法,并且可以避免程序员指定冗余的类型信息。例如,给定方法声明

class Util
{
static Random rand = new Random();
static public T Choose<T>(T first , T second)
{
return (rand.Next(2) == 0)?first:second
}
}
Copy after login

不显式指定类型实参而调用 Choose方法也是可以的。

int i = Util.Choose(5,123); //调用Choose<int>
string s = Util.Choose(“foo”,”bar”);//调用Choose<string>
Copy after login

通过类型推断,类型实参int和string 将由方法的实参确定。

类型推断作为方法调用(§20.9.5)编译时处理的一部分而发生,并且在调用的重载决策之前发生。当在一个方法调用中指定特定的方法组时,类型实参不会作为方法调用的一部分而指定,类型推断将被应用到方法组中的每个泛型方法。如果类型推断成功,被推断的类型实参将被用于确定后续重载决策的实参类型。如果重载决策选择将要调用的泛型方法,被推断的类型实参将被用作调用的实际类型实参。如果特定方法类型推断失败,这个方法将不参与重载决策。类型推断失败自身将不会产生编译时错误。但当重载决策没能找到适用的方法时,将会导致编译时错误。

如果所提供的实参个数与方法的参数个数不同,推断将立刻失败。否则,类型推断将为提供给方法的每个正式实参独立地发生。假定这个实参的类型为A,对应参数为类型P。类型推断将按下列步骤关联类型A和P而。

如果以下任何一条成立,将不能从实参推断任何东西(但类型推断是成功的)

- P和方法的任何类型参数无关[1]
- 实参是null字符
- 实参是一个匿名方法
- 实参是一个方法组



如果P是一个数组类型,A是一个同秩(rank)的数组类型,那么使用A和P的元素类型相应地替换A和P,并重复这个步骤。

如果P是一个数组类型,而A 是一个不同秩的数组类型,那么泛型方法的类型推断失败。

如果P是方法的类型参数,那么对于这个实参的类型推断成功,并且A是那个类型实参所推断的类型。

否则,P必须是一个构造类型。如果对于出现在P中的每个方法类型参数MX ,恰好可以确定一个类型TX,使用每个TX替换每个MX ,这将产生一个类型,对于这个类型,A可以通过标准的隐式转换而被转换,那么对于这个实参的类型推断成功,对于每个MX,TX 就是推断的类型。方法类型参数约束(如果有的话),因为类型推断的原因将会被忽略。如果对于一个给定的MX 没有TX存在或者多于一个TX存在 ,那么泛型方法的类型推断将会失败(多于一个TX 存在的情形只可能发生在,P是一个泛型接口类型,并且A实现了接口的多个构造版本)。

如果所有的方法实参都通过先前的算法进行了处理,那么从实参而来的所有推断都将被汇聚。这组推断必须有如下的属性。

方法的每个类型参数必须有一个为其推断的类型实参。简而言之,这组推断必须是完整的([/b]complete[/b])[/b];

如果类型参数出现多于一次,那么对那个类型参数的所有的推断都必须推断相同的类型实参。简而言之,这组接口必须是一致的([/b]consistent[/b])[/b]。


如果能够找到一组完整而一致的推断类型实参,那么对于一个给定的泛型方法和实参列表,类型推断就可以说是成功的。

如果泛型方法使用参数数组(§10.5.1.4)声明,那么类型推断针对方法将以其通常的方式执行。如果类型推断成功,结果方法是可用的,那么方法将以其通常形式对于重载决策是可行的。否则,类型推断将针对方法的扩展形式(§7.4.2.1)执行。

[1]也就是P并不是方法的类型参数列表所列类型之一。

以上就是C# 2.0 Specification (泛型四)的内容,更多相关内容请关注PHP中文网(www.php.cn)!


Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!