首页 > 开发 > 综合 > 正文

c#v2.0 扩展特性 翻译(2)

2024-07-21 02:19:37
字体:
来源:转载
供稿:网友
generic type instantiations
范型实例化

similar to a non-generic type, the compiled representation of a generic type is intermediate language (il) instructions and metadata. the representation of the generic type of course also encodes the existence and use of type parameters.

和非泛型类似,泛型被编译后表示成中间代码指令和元数据。泛型的表示当然也是将已有的和使用的类型参数编码。



the first time an application creates an instance of a constructed generic type, such as stack<int>, the just-in-time (jit) compiler of the .net common language runtime converts the generic il and metadata to native code, substituting actual types for type parameters in the process. subsequent references to that constructed generic type then use the same native code. the process of creating a specific constructed type from a generic type is known as a generic type instantiation.

当应用程序第一次创建一个新的被构造的泛型,例如stack<int>,.net公共运行时的jit将泛型的中间代码和元数据转化成本地代码,在进程中用真实类型取代类型参数。后来引用已经被构建的泛型就运行本地代码。根据指定的构建类型来创建泛型被称作泛型的实例化。



the .net common language runtime creates a specialized copy of the native code for each generic type instantiation with a value type, but shares a single copy of the native code for all reference types (since, at the native code level, references are just pointers with the same representation).

.net 公共语言运行时为每个值类型的泛型创建一个专门的本地代码拷贝。但会为所有引用类型共享一个单独的本地代码拷贝。(因为,在本地代码层次,引用和指针就是同一表示)



19.1.2 constraints
约束



commonly, a generic class will do more than just store data based on a type parameter. often, the generic class will want to invoke methods on objects whose type is given by a type parameter. for example, an add method in a dictionary<k,v> class might need to compare keys using a compareto method:

一般来说,一个泛型类不仅可以存储建立在类型参数上的数据,还能做更多。通常,泛型类会尝试调用被指定类型对象上的方法。举例说,在dictionary<k,v>类中一个add方法可能需要通过compareto方法比较关键字。

public class dictionary<k,v>
{
public void add(k key, v value)
{
...

if (key.compareto(x) < 0) {...} // error, no compareto method
...
}
}

since the type argument specified for k could be any type, the only members that can be assumed to exist on the key parameter are those declared by type object, such as equals, gethashcode, and tostring; a compile-time error therefore occurs in the example above. it is of course possible to cast the key parameter to a type that contains a compareto method. for example, the key parameter could be cast to icomparable:

然而类型参数k可能是任何类型,被假定存在于key参数的唯一成员变量是那些object类型所声明的,比如说 equal,gethashcode和tostring ;上面的代码将引发一个编译时错误。当然也可以把key

参数转化成一个包含compareto方法的类型。例如,key参数可能被转化成支持icomparable接口。

public class dictionary<k,v>
{
public void add(k key, v value)
{
...

if (((icomparable)key).compareto(x) < 0) {...}
...
}
}

while this solution works, it requires a dynamic type check at run-time, which adds overhead. it furthermore defers error reporting to run-time, throwing an invalidcastexception if a key doesn’t implement icomparable.

当以上解决方案运行时,加在上面的代码要求进行一个运行时的动态类型检查。而且它在运行时才报告错误,并在当key不支持icomparable接口时会抛出一个invalidcastexception.



to provide stronger compile-time type checking and reduce type casts, c# permits an optional list of constraints to be supplied for each type parameter. a type parameter constraint specifies a requirement that a type must fulfill in order to be used as an argument for that type parameter. constraints are declared using the word where, followed by the name of a type parameter, followed by a list of class or interface types, or the constructor constraint new().

为了提供更强的编译时类型检查和减少类型转换,c#允许一个可选择的约束列表去提供给每一个类型参数。作为一个类型参数约束的要求,一个类型参数约束指定一个必须完全履行的类型。约束通过关键字where声明,后面跟上类型参数的名字,再跟上一串类或接口,或是一个约束构造器new()

in order for the dictionary<k,v> class to ensure that keys always implement icomparable, the class declaration can specify a constraint for the type parameter k:

为了保证dictionary<k,v>类的key支持icomparable接口,类的声明指定一个类型参数的约束。



public class dictionary<k,v> where k: icomparable
{
public void add(k key, v value)
{
...

if (key.compareto(x) < 0) {...}
...
}
}

given this declaration the compiler will ensure that any type argument supplied for k is a type that implements icomparable. furthermore, it is no longer necessary to explicitly cast the key parameter to icomparable before calling the compareto method; all members of a type given as a constraint for a type parameter are directly available on values of that type parameter type.

指定以后,编译器将确保任何提供给k的类型参数必须支持icomparable接口。而且,在调用compareto 方法前,它不再需要显式的把key参数转化支持icomparable接口。被一个类型参数的约束所给定的所有成员在类型值上直接可见。



for a given type parameter, it is possible to specify any number of interfaces as constraints, but no more than one class. each constrained type parameter has a separate where clause. in the example below, the type parameter k has two interface constraints, while the type parameter e has a class constraint and a constructor constraint:

作为一个给定的类型参数,但对一个类可能会指定任何数量的接口作为约束。每种约束类型参数有一个where子句分隔。在下面的例子里,类型参数k有两个接口约束,然而类型参数e有一个类约束和一个构建约束。

public class entitytable<k,e>
where k: icomparable<k>, ipersistable
where e: entity, new()
{
public void add(k key, e entity)
{
...

if (key.compareto(x) < 0) {...}
...
}
}

the constructor constraint, new(), in the example above ensures that a type used as a type argument for e has a public, parameterless constructor, and it permits the generic class to use new e() to create instances of that type.

上面例子中的构建约束new(),确保作为类型参数e的类型有一个公共的,无参数的构建函数,且它允许泛型类 使用 new e() 去创建该类型的实例。



type parameter constrains should be used with care. while they provide stronger compile-time type checking and in some cases improve performance, they also restrict the possible uses of a generic type. for example, a generic class list<t> might constrain t to implement icomparable such that the list’s sort method can compare items. however, doing so would preclude use of list<t> for types that don’t implement icomparable, even if the sort method is never actually called in those cases.

类型参数约束应该小心使用。他们提供更强的编译时类型检查并能在某些例子来提高性能,也能约束泛型的使用可能性。举例说,一个list<t>泛型可能会约束t实现icomparable接口,这样list的sort方法就可以比较项目。然而,这将使不支持icomparable接口的类型不能使用list<t>,即使sort方法没有在案例中被调用。


商业源码热门下载www.html.org.cn

发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表