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

Introduction to C# 2.0
C# 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没有加任何新的可能会在现有代码冲突的关键词

Generics
Generics 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 error
int x = stack.Pop(); // Type mismatch error

Generic 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"];

转载于:https://www.cnblogs.com/huiseguding/archive/2006/07/05/443787.html

c#v2.0 扩展特性 翻译(1)相关推荐

  1. 即时通讯(IM)开源项目OpenIM每周迭代版本发布-音视频实时通话-v2.0.4

    介绍 OpenIM每周五发布新版,包括新特性发布,bug修复,同时合并PR 由于2.0版本重构完毕,架构更清晰,代码更规范,先邀请各位参与OpenIM社区建设,包括技术开发,技术分享等,特性开发,性能 ...

  2. XXL-JOB v2.0.2,分布式任务调度平台 | 多项特性优化更新

    开发四年只会写业务代码,分布式高并发都不会还做程序员?   v2.0.2 Release Notes 1.底层通讯方案优化:升级较新版本xxl-rpc,由"JETTY"方案调整为& ...

  3. xxl子任务_XXL-JOB v2.0.2,分布式任务调度平台 | 多项特性优化更新

    v2.0.2 Release Notes 1.底层通讯方案优化:升级较新版本xxl-rpc,由"JETTY"方案调整为"NETTY_HTTP"方案,执行器内嵌n ...

  4. Android4.0新特性 中文翻译

    转自http://www.eoeandroid.com/thread-103300-1-1.html android4.0 SDK发布有一段时间了,在eoe上找到了翻译过的新特性说明,特转载 Andr ...

  5. 大数据技术之_11_HBase学习_03_HBase 实战之谷粒微博(练习API) + 扩展知识(布隆过滤器+HBase2.0 新特性)

    大数据技术之_11_HBase学习_03 第8章 HBase 实战之谷粒微博 8.1 需求分析 8.2 代码实现 第9章 扩展知识 9.1 HBase 在商业项目中的能力 9.2 布隆过滤器 9.3 ...

  6. C# 3.0新特性之扩展方法

    C#3.0扩展方法是给现有类型添加一个方法.现在类型既可是基本数据类型(如int,String等),也可以是自己定义的类. //Demo--1 //扩展基本类型 namespace TestExten ...

  7. [翻译] C# 8.0 新特性

    原文: Building C# 8.0 [译注:原文主标题如此,但内容大部分为新特性介绍,所以意译标题为 "C# 8.0 新特性"] C# 的下一个主要版本是 8.0.我们已经为它 ...

  8. RT-Thread Studio V2.0发布啦!新功能新特性等你来体验!

    时光如梭,一眨眼离V1.1.5版本发布已经过去一个多月了,2020年也进入了最后一个月的倒计时,你们翘首以盼的Studio新版本终于发布啦!感谢一直使用和关注Studio的工程师们,感谢你们一直热心地 ...

  9. Android 7.0 新特性

    注:本文是从谷歌官网翻译过来的,放这里便于查阅和消化理解,更多详细知识点请自备梯子访问官网~ API 级别:24 代号:N Android 7.0 Nougat 为用户和开发者引入多种新功能.本文重点 ...

最新文章

  1. 酷炫Jquery收集
  2. 【288天】每日项目总结系列026(2017.11.20)
  3. c# 基于layui的通用后台管理系统_【SpringBoot】三十三、SpringBoot+LayUI后台管理系统开发脚手架...
  4. linux命令 - alias
  5. Log4Net配置使用简记
  6. spark数据本地性级别划分
  7. spark 类别特征_spark 机器学习基础 数据类型
  8. IE11离线安装总是提示“获取更新”的解决方法
  9. 比navicat还好用的mysql可视化工具
  10. 从零开始封装windows10 1803 超详细图文分享 第三篇:程序的安装与优化
  11. SitePoint播客#43:被动共享
  12. 我看朴灵评注阮一峰的《JavaScript 运行机制详解:再谈Event Loop》
  13. 上岸快手,我选择一条不一样的路
  14. IKVM.NET的妙手偶得
  15. opencv 识别黄色物体的程序代码
  16. “枪枪爆头”!用Python写个了使命召唤外挂
  17. 使用Centreon监控HP惠普服务器硬件状态
  18. 服务器硬盘选金盘好吗,WD 西部数据 企业级 10T金盘与8T金盘 对比测试
  19. 2020校招笔试之新华三
  20. android 破解软件

热门文章

  1. python调用php命令行,python调用php函数 python怎样调用php文件中的函数详解
  2. bilibili怎么设置弹幕数量_python爬取B站视频弹幕分析并制作词云
  3. awk,sed,cut获取最后一列数据
  4. 公有云厂商DDoS防护产品竞品分析——内含CC的一些简单分析,貌似多是基于规则,CC策略细粒度ip/url//ua/refer...
  5. 一种神经元探索系统方法及装置
  6. DNS解析污染原理——要么修改包,要么直接丢弃你的网络包
  7. 机器学习特征表达——日期与时间特征做离散处理(数字到分类的映射),稀疏类分组(相似特征归档),创建虚拟变量(提取新特征) 本质就是要么多变少,或少变多...
  8. 兼容ie9以下css3,hover和圆角(htc)
  9. 兼容ie跟谷歌上传文件
  10. THinkPHP 5.0 域名路由