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

翻译 2004年07月22日 11:11:00

 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类型所声明的,比如说 EqualGetHashCodeToString ;上面的代码将引发一个编译时错误。当然也可以把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接口,这样ListSort方法就可以比较项目。然而,这将使不支持IComparable接口的类型不能使用List<T>,即使Sort方法没有在案例中被调用。

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

  • zgqtxwd
  • zgqtxwd
  • 2008-05-01 04:07:00
  • 77

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

   Generic type instantiations 范型实例化 Similar to a non-generic type, the compiled representation of a...
  • lovered
  • lovered
  • 2004-07-22 17:06:00
  • 826

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

 Introduction to C# 2.0C# 2.0 introduces several language extensions, the most important of which ar...
  • eshao
  • eshao
  • 2004-07-19 16:37:00
  • 1192

c#v2.0 扩展特性(1)

   Introduction to C# 2.0 C# 2.0 introduces several language extensions, the most important of which...
  • lovered
  • lovered
  • 2004-07-22 17:04:00
  • 907

扩展VLAN特性与管理——3

QinQ映射配置与管理 QinQ映射功能可以将用户的VLAN标签映射为指定的VLAN标签,从而起到屏蔽不同用户VLAN标签的作用。但QinQ映射功能只能在子接口上应用,通过QinQ映射功能,子接口在向...
  • kaoa000
  • kaoa000
  • 2017-07-07 13:25:37
  • 288

给Mac系统浏览器扩展翻译功能

Google Chrome可以即时翻译网页。可是,如果你用惯了Safari,不想安装Google Chrome,又需要对外文网页即时翻译,那么,请看下面: 前提:先打开Safari,点击“显示-显示个...
  • u010070526
  • u010070526
  • 2015-07-08 14:16:20
  • 1807

VS2008编译出现问题:error C2485: “__restrict”: 无法识别的扩展属性 解决办法

VS2008编译出现问题:error C2485: “__restrict”: 无法识别的扩展属性 解决办法 错误:Error3 error C2485: '__restrict' : unre...
  • swanabin
  • swanabin
  • 2014-01-06 12:18:41
  • 3506

Google翻译2017拓展crx文件

  • 2017年09月26日 08:44
  • 212KB
  • 下载

请确保此代码文件中定义的类与“inherits”特性匹配,并且该类扩展的基类(例如 Page 或 UserControl)是正确的。

可能的原因: 1、前台页面.aspx中Inherits的命名与其代码隐藏文件.aspx.cs中的类名不同。 2、代码隐藏文件中手动添加了命名空间,去掉后可编译正常(建议修改在.aspx页面中的in...
  • Scorpion_Walter
  • Scorpion_Walter
  • 2015-04-08 23:16:37
  • 2054

PDO概述特点及安装

PDO概述:     PDO是PHP Data Object (PHP数据对象)的简称,它是与PHP5.1版本一起发行的。目前支持的数据库包括Firebird、FreeTDS、Interbase、M...
  • qq_28602957
  • qq_28602957
  • 2016-03-29 16:49:23
  • 1765
收藏助手
不良信息举报
您举报文章:c#v2.0 扩展特性 翻译(2)
举报原因:
原因补充:

(最多只允许输入30个字)