Introduction to C# 2.0
Introduction to C# 2.0C# 2.0 introduces several language extensions, the most important of which are Generics, Anonymous Methods, Iterators, and Partial Types. C#2.0 介绍几种语言扩展,泛型,匿名方法,迭代器 和、partial Types. · Generics permit classes, structs, interfaces, delegates, and methods to be parameterized by the types of data they store and manipulate. Generics are useful because they provide stronger compile-time type checking, require fewer explicit conversions between data types, and reduce the need for boxing operations and run-time type checks. 泛型允许类,结构,接口,代理还有方法被他们存储和操作数据类型参数化。泛型相当有用,因为他们提供强制的编译时类型检查,要求更少的数据类型之间的显式转换,并减少装箱拆箱的操作和运行时类型检查。 · Anonymous methods allow code blocks to be written “in-line” where delegate values are expected. Anonymous methods are similar to lambda functions in the Lisp programming language. C# 2.0 supports the creation of “closures” where anonymous methods access surrounding local variables and parameters. · Iterators are methods that incrementally compute and yield a sequence of values. Iterators make it easy for a type to specify how the foreach statement will iterate over its elements. · Partial types allow classes, structs, and interfaces to be broken into multiple pieces stored in different source files for easier development and maintenance. Additionally, partial types allow separation of machine-generated and user-written parts of types so that it is easier to augment code generated by a tool. This chapter gives an introduction to these new features. Following the introduction are four chapters that provide a complete technical specification of the features. 这个章节将介绍这些新特性。随后的四个章节的介绍将提供有关特性的完整技术规范 The language extensions in C# 2.0 were designed to ensure maximum compatibility with existing code. For example, even though C# 2.0 gives special meaning to the words where, yield, and partial in certain contexts, these words can still be used as identifiers. Indeed, C# 2.0 adds no new keywords as such keywords could conflict with identifiers in existing code. c#2.0中的语言扩展最大程度上保证和现有代码的兼容。举例说,即使c#2.0指定以下词如yield,partial在特定的上下文有特有的意义,他们依然可以作为标示符。甚至,c#2.0没有加任何新的可能会在现有代码冲突的关键词 GenericsGenerics permit classes, structs, interfaces, delegates, and methods to be parameterized by the types of data they store and manipulate. C# generics will be immediately familiar to users of generics in Eiffel or 泛型允许类,结构,接口,代理和方法被他们存储操作的数据类型参数化。C#泛型将很快被eiffel,ada的使用过泛型的用户熟悉,或是使用过c++templates,尽管他们不需要忍受以后的多种编译器。 Why generics?Without generics, general purpose data structures can use type object to store data of any type. For example, the following simple Stack class stores its data in an object array, and its two methods, Push and Pop, use object to accept and return data, respectively: 如果没有泛型,一般数据结构能使用类型对象去存储任何数据类型。举例,下面所描述的一个很简单的栈的类存储数据在对象数组中。它有两个方法push和pop,使用对象分别地去接受和返回数据 public class Stack public void Push(object item) {...} public object Pop() {...} While the use of type object makes the Stack class very flexible, it is not without drawbacks. For example, it is possible to push a value of any type, such a Customer instance, onto a stack. However, when a value is retrieved, the result of the Pop method must explicitly be cast back to the appropriate type, which is tedious to write and carries a performance penalty for run-time type checking: 当使用对象类型的时候栈类的使用更灵活,它并非没有缺陷。举例说,它很可能压入任何类型的值,如一个customer实例到一个栈。然而,当一个值返回,pop方法返回的结果必须显式转化成适当类型,不但编写是乏味的并且在运行时的类型检查降低性能。 Stack stack = new Stack(); If a value of a value type, such as an int, is passed to the Push method, it is automatically boxed. When the int is later retrieved, it must be unboxed with an explicit type cast: 如果是一个值类型,如整型传入push方法,它自动装箱。当整型在后来返回的时候,它必须进行显式的拆箱。 Stack stack = new Stack(); Such boxing and unboxing operations add performance overhead since they involve dynamic memory allocations and run-time type checks. 当他们处于动态内存分配和运行时类型检查,装箱拆箱操作将增加性能消耗。 A further issue with the Stack class is that it is not possible to enforce the kind of data placed on a stack. Indeed, a Customer instance can be pushed on a stack and then accidentally cast it to the wrong type after it is retrieved: 进一步的关于栈的讨论,强迫数据的类别压入到栈是不可能的。事实上,一个customer实例能被压入栈并且很有可能偶然在它返回时被转化成错误的类型。 Stack stack = new Stack(); While the code above is an improper use of the Stack class, the code is technically speaking correct and a compile-time error is not reported. The problem does not become apparent until the code is executed, at which point an InvalidCastException is thrown. The Stack class would clearly benefit from the ability to specify its element type. With generics, that becomes possible. 以上代码从技术上说是正确的且在编译时是不会报错的,但对stack类的用法是不正确的。这个问题直到代码执行才会显示出来,并抛出InvalidCastException异常. 栈类应当受益于指定元素类型的能力。有了泛型以后,这个将成为可能。 Creating and using generics 创建和使用泛型 Generics provide a facility for creating types that have type parameters. The example below declares a generic Stack class with a type parameter T. The type parameter is specified in < and > 泛型提供了一个便利的方法,通过类型参数去创建类型。下面的例子通过类型参数T声明了一个泛型的栈。类型参数在类名后面<>分隔符中定义。Stack public class Stack public void Push(T item) {...} public T Pop() {...} When the generic class Stack 当泛型类Stack Stack The Stack Stack Generics provide strong typing, meaning for example that it is an error to push an int onto a stack of Customer objects. Just as a Stack 泛型提供强类型,意味着举例说来 将一个整型数据压入Customer 泛类型的栈。正像一个int泛型栈被严格约束只能操作int型,因此Customer型被严格要求操作Customer对象。下面例子的最后两行,编译器编译的时候会报告错误。 Stack Generic type declarations may have any number of type parameters. The Stack 泛型声明可以包括任何数目的类型参数。上面Stack public class Dictionary public V this[K key] {...} When Dictionary 当Dictionary Dictionary |