当前位置: 首页 > 图文教程 > 网络编程 > ASP.NET > c#v2.0 扩展特性 翻译2

ASP.NET
Script:WINDOWS Script 枚举运行中进程
使用Flex结合Webservice完成域名查询
VSTS Team System 总算装好了。
用于部署数据库的 数据库初始化工具 xzSQLDeploy Tools V1.0 (for SQLServer) f...
一个将阿拉伯数字转换成中文大写的最简单算法
SCRIPT:使用Windows Script 关闭和打开指定程序
Script:使用WINDOWS脚本访问WEB SERVICES
asp.net连接Access数据库
VB中IIS Application发布可能出现的问题
VB打包后的安装问题
Nhibernate的数据分页技术(续)
使用API函数复制文件,可显示进度。
VB打包技巧
VB.NET实现DirectSound9 (9) 实现示波器
VB.NET 实现DirectSound9 (10) 均衡器
[水晶报表部署系列之一]轻松搞定水晶报表9.2打包
DataGrid 中双向排序的一种办法
利用System.EventHandler来实现两个窗体间的事件调用
多线程应用程序中调用窗体的一点心得
Smart Client之旅一:用B/S方式运行Exe应用程序

ASP.NET 中的 c#v2.0 扩展特性 翻译2


出处:互联网   整理: 软晨网(RuanChen.com)   发布: 2009-11-03   浏览: 59 ::
收藏到网摘: n/a

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, 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,.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 class might need to compare keys using a CompareTo method: 一般来说,一个泛型类不仅可以存储建立在类型参数上的数据,还能做更多。通常,泛型类会尝试调用被指定类型对象上的方法。举例说,在Dictionary类中一个Add方法可能需要通过CompareTo方法比较关键字。 public class Dictionary{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{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#允许一个可选择的约束列表去提供给每一个类型参数。作为一个类型参数约束的要求,一个类型参数约束指定一个必须完全履行的类型。