大雀软件园

首页 软件下载 安卓市场 苹果市场 电脑游戏 安卓游戏 文章资讯 驱动下载
技术开发 网页设计 图形图象 数据库 网络媒体 网络安全 站长CLUB 操作系统 媒体动画 安卓相关
当前位置: 首页 -> 技术开发 -> NET专区 -> c#v2.0 扩展特性 翻译(1)

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

时间: 2021-07-31 作者:daque

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 ada, or to users of c++ templates, though they do not suffer many of the complications of the latter.泛型承诺类,构造,接口,代劳和本领被她们保存操纵的数据典型参数化。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{object[] items;int count;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();stack.push(new customer());customer c = (customer)stack.pop();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();stack.push(3);int i = (int)stack.pop();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();stack.push(new customer());string s = (string)stack.pop();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 > delimiters after the class name. rather than forcing conversions to and from object, instances of stack accept the type for which they are created and store data of that type without conversion. the type parameter t acts as a placeholder until an actual type is specified at use. note that t is used as the element type for the internal items array, the type for the parameter to the push method, and the return type for the pop method:泛型供给了一个便当的本领,经过典型参数去创造典型。底下的例子经过典型参数t声领会一个泛型的栈。典型参数在类名反面<>分割符中设置。stack范例接收它创造和保存的数据典型而不须要变换远胜似强迫的东西变换。public class stack{t[] items;int count;public void push(t item) {...}public t pop() {...}}when the generic class stack is used, the actual type to substitute for t is specified. in the following example, int is given as the type argument for t:当泛型类stack被运用,代替t的如实典型将被指定。底下的例子里,int 被指定包办t。stack stack = new stack();stack.push(3);int x = stack.pop();the stack type is called a constructed type. in the stack type, every occurrence of t is replaced with the type argument int. when an instance of stack is created, the native storage of the items array is an int[] rather than object[], providing substantial storage efficiency compared to the non-generic stack. likewise, the push and pop methods of a stack operate on int values, making it a compile-time error to push values of other types onto the stack, and eliminating the need to explicitly cast values back to their original type when they’re retrieved.stack典型被称作结构典型. 在stack里,历次展示t将被典型参数int包办。当一个stack范例被创造,自己保存items数组是一个int型数组,比非泛型栈的东西数组供给如实性保存功效。同样的,push和pop本领操纵int 值,即使push其余典型的数据给栈,将引导一个编写翻译时缺点,它废除了当班值日被归来时所诉求的显式变化成原形。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 is restricted to operate only on int values, so is stack restricted to customer objects, and the compiler will report errors on the last two lines of the following example:泛型供给强典型,表示着举例说来 将一个整型数据压入customer 泛典型的栈。正像一个int泛型栈被庄重牵制只能操纵int型,所以customer型被庄重诉求操纵customer东西。底下例子的结果两行,编写翻译器编写翻译的功夫会汇报缺点。stack stack = new stack();stack.push(new customer());customer c = stack.pop();stack.push(3); // type mismatch errorint x = stack.pop(); // type mismatch errorgeneric type declarations may have any number of type parameters. the stack example above has only one type parameter, but a generic dictionary class might have two type parameters, one for the type of the keys and one for the type of the values:泛型证明不妨囊括任何数手段典型参数。上头stack的例子只是惟有一个典型参数,但泛型dictionary类不妨含有两个典型参数,一个是要害字的典型,一个是值的典型。public class dictionary{public void add(k key, v value) {...}public v this[k key] {...}}when dictionary is used, two type arguments would have to be supplied:当dictionary被运用,必需供给两种典型参数dictionary dict = new dictionary();dict.add("peter", new customer());customer c = dict["peter"];

热门阅览

最新排行

Copyright © 2019-2021 大雀软件园(www.daque.cn) All Rights Reserved.