C# 发展历史The history of C#

本页介绍了 C# 语言每个主要版本的发展历史。This article provides a history of each major release of the C# language. C# 团队将继续创新,以添加新功能。The C# team is continuing to innovate and add new features. 可以在 GitHub 上的 dotnet/roslyn 存储库上找到详细的语言功能状态,包括考虑在即将发布的版本中添加的功能。Detailed language feature status, including features considered for upcoming releases can be found on the dotnet/roslyn repository on GitHub.

重要

为了提供一些功能,C# 语言依赖 C# 规范定义为标准库 所用的类型和方法。The C# language relies on types and methods in what the C# specification defines as a standard library for some of the features. .NET 平台通过许多包交付这些类型和方法。The .NET platform delivers those types and methods in a number of packages. 例如,异常处理。One example is exception processing. 为了确保引发的对象派生自 Exception,将会检查每个 throw 语句或表达式。Every throw statement or expression is checked to ensure the object being thrown is derived from Exception. 同样,还会检查每个 catch,以确保捕获的类型派生自 ExceptionSimilarly, every catch is checked to ensure that the type being caught is derived from Exception. 每个版本都可能会新增要求。Each version may add new requirements. 若要在旧版环境中使用最新语言功能,可能需要安装特定库。To use the latest language features in older environments, you may need to install specific libraries. 每个特定版本的页面中记录了这些依赖项。These dependencies are documented in the page for each specific version. 若要了解此依赖项的背景信息,可以详细了解语言与库的关系You can learn more about the relationships between language and library for background on this dependency.

C# 生成工具将最新的主要语言版本视为默认语言版本。The C# build tools consider the latest major language release the default language version. 主要版本之间可能有单点修正发行版。有关详细信息,请参阅本节中的其他文章。There may be point releases between major releases, detailed in other articles in this section. 若要使用单点版本中的最新功能,需要配置编译器语言版本并选择版本。To use the latest features in a point release, you need to configure the compiler language version and select the version. 自 C# 7.0 起,已有三个单点修正发行版:There have been three-point releases since C# 7.0:

C# 1.0 版C# version 1.0

回想起来,和 Visual Studio .NET 2002 一起发布的 C# 版本 1.0 非常像 Java。When you go back and look, C# version 1.0, released with Visual Studio .NET 2002, looked a lot like Java. ECMA 制定的设计目标中,它旨在成为一种“简单、现代、面向对象的常规用途语言”。As part of its stated design goals for ECMA, it sought to be a "simple, modern, general-purpose object-oriented language." 当时,它和 Java 类似,说明已经实现了上述早期设计目标。At the time, looking like Java meant it achieved those early design goals.

不过如果现在回顾 C# 1.0,你会觉得有点晕。But if you look back on C# 1.0 now, you'd find yourself a little dizzy. 它没有习以为常的内置异步功能和以泛型为中心的巧妙功能。It lacked the built-in async capabilities and some of the slick functionality around generics you take for granted. 其实它完全不具备泛型。As a matter of fact, it lacked generics altogether. LINQ 呢?And LINQ? 尚不可用。Not available yet. 这些新增内容需要几年才能推出。Those additions would take some years to come out.

与现在的 C# 相比,C# 1.0 版少了很多功能。C# version 1.0 looked stripped of features, compared to today. 你会发现自己的代码很冗长。You'd find yourself writing some verbose code. 不过凡事总要有个开始。But yet, you have to start somewhere. 在 Windows 平台上,C# 1.0 版是 Java 的一个可行的替代之选。C# version 1.0 was a viable alternative to Java on the Windows platform.

C# 1.0 的主要功能包括:The major features of C# 1.0 included:

C# 版本 1.2C# version 1.2

随 Visual Studio .NET 2003 一起提供的 C# 版本 1.2。C# version 1.2 shipped with Visual Studio .NET 2003. 它对语言做了一些小改进。It contained a few small enhancements to the language. 最值得注意的是,从此版本开始,当 IEnumerator 实现 IDisposable 时,foreach 循环中生成的代码会在 IEnumerator 上调用 DisposeMost notable is that starting with this version, the code generated in a foreach loop called Dispose on an IEnumerator when that IEnumerator implemented IDisposable.

C# 2.0 版C# version 2.0

从此以后事情变得有趣起来。Now things start to get interesting. 让我们看看 C# 2.0(2005 年发布)和 Visual Studio 2005 中的一些主要功能:Let's take a look at some major features of C# 2.0, released in 2005, along with Visual Studio 2005:

除现有功能以外的其他 C# 2.0 功能:Other C# 2.0 features added capabilities to existing features:

  • getter/setter 单独可访问性Getter/setter separate accessibility
  • 方法组转换(委托)Method group conversions (delegates)
  • 静态类Static classes
  • 委托推断Delegate inference

C# 一开始是面向对象的 (OO) 通用语言,而 C# 2.0 版很快改变了这一点。While C# may have started as a generic Object-Oriented (OO) language, C# version 2.0 changed that in a hurry. 做好基础准备后,他们开始追求解决一些严重影响开发者的难点。Once they had their feet under them, they went after some serious developer pain points. 且他们以显著的方式追求这些难点。And they went after them in a significant way.

通过泛型,类型和方法可以操作任意类型,同时保持类型安全性。With generics, types and methods can operate on an arbitrary type while still retaining type safety. 例如,通过 List<T>,将获得 List<string>List<int> 并且可以对这些字符串或整数执行类型安全操作,同时对其进行循环访问。For instance, having a List<T> lets you have List<string> or List<int> and perform type-safe operations on those strings or integers while you iterate through them. 使用泛型优于创建派生自 ArrayListListInt 或者从每个操作的 Object 强制转换。Using generics is better than create ListInt that derives from ArrayList or casting from Object for every operation.

C# 2.0 版引入了迭代器。C# version 2.0 brought iterators. 简单来说,迭代器允许使用 foreach 循环来检查 List(或其他可枚举类型)中的所有项。To put it succinctly, iterators let you examine all the items in a List (or other Enumerable types) with a foreach loop. 拥有迭代器是该语言最重要的一部分,显著提升了语言的可读性以及人们推出代码的能力。Having iterators as a first-class part of the language dramatically enhanced readability of the language and people's ability to reason about the code.

不过 C# 依然在追赶 Java 的道路上。And yet, C# continued to play a bit of catch-up with Java. 当时 Java 已发布包含泛型和迭代器的版本。Java had already released versions that included generics and iterators. 但是随着语言各自的演化,形势很快发生了变化。But that would soon change as the languages continued to evolve apart.

C# 3.0 版C# version 3.0

C# 3.0 版和 Visual Studio 2008 一起发布于 2007 年下半年,但完整的语言功能是在 .NET Framework 3.5 版中发布的。C# version 3.0 came in late 2007, along with Visual Studio 2008, though the full boat of language features would actually come with .NET Framework version 3.5. 此版本标示着 C# 发展过程中的重大更改。This version marked a major change in the growth of C#. C# 成为了真正强大的编程语言。It established C# as a truly formidable programming language. 我们来看看此版本中的一些主要功能:Let's take a look at some major features in this version:

回顾过去,这些功能中大多数似乎都是不可或缺,难以分割的。In retrospect, many of these features seem both inevitable and inseparable. 它们的组合都是经过巧妙布局。They all fit together strategically. 我们通常认为 C# 版本的杀手锏是查询表达式,也就是语言集成查询 (LINQ)。It's generally thought that C# version's killer feature was the query expression, also known as Language-Integrated Query (LINQ).

LINQ 的构造可以建立在更细微的视图检查表达式树、Lambda 表达式以及匿名类型的基础上。A more nuanced view examines expression trees, lambda expressions, and anonymous types as the foundation upon which LINQ is constructed. 不过无论如何 C# 3.0 都提出了革命性的概念。But, in either case, C# 3.0 presented a revolutionary concept. C# 3.0 开始为 C# 转变为面向对象/函数式混合语言打下基础。C# 3.0 had begun to lay the groundwork for turning C# into a hybrid Object-Oriented / Functional language.

具体来说,你现在可以编写 SQL 样式的声明性查询对集合以及其他项目执行操作。Specifically, you could now write SQL-style, declarative queries to perform operations on collections, among other things. 无需再编写 for 循环来计算整数列表的平均值,现在可改用简单的 list.Average() 方法。Instead of writing a for loop to compute the average of a list of integers, you could now do that as simply as list.Average(). 组合使用查询表达式和扩展方法让各种数字变得智能多了。The combination of query expressions and extension methods made it look as though that list of integers had gotten a whole lot smarter.

人们需要一些时间来掌握和吸收这种概念,不过已经逐渐做到了。It took time for people to really grasp and integrate the concept, but they gradually did. 现在又过了几年,代码变得更简洁,功能也更强大了。And now, years later, code is much more concise, simple, and functional.

C# 4.0 版C# version 4.0

C# 版本 4.0 随 Visual Studio 2010 一起发布,很难达到版本 3.0 的创新水平。C# version 4.0, released with Visual Studio 2010, would have had a difficult time living up to the groundbreaking status of version 3.0. 在 3.0 版中,C# 已经完全从 Java 的阴影中脱颖而出,崭露头角。With version 3.0, C# had moved the language firmly out from the shadow of Java and into prominence. 很快成为一种简洁精炼的语言。The language was quickly becoming elegant.

下一版本引入了一些有趣的新功能:The next version did introduce some interesting new features:

嵌入的互操作类型缓和了部署难点。Embedded interop types alleviated a deployment pain. 泛型协变和逆变提供了更强的功能来使用泛型,但风格比较偏学术,应该最受框架和库创建者的喜爱。Generic covariance and contravariance give you more power to use generics, but they're a bit academic and probably most appreciated by framework and library authors. 命名参数和可选参数帮助消除了很多方法重载,让使用更方便。Named and optional parameters let you eliminate many method overloads and provide convenience. 但是这些功能都没有完全改变模式。But none of those features are exactly paradigm altering.

主要功能是引入 dynamic 关键字。The major feature was the introduction of the dynamic keyword. 在 C# 4.0 版中引入 dynamic 关键字让用户可以替代编译时类型上的编译器。The dynamic keyword introduced into C# version 4.0 the ability to override the compiler on compile-time typing. 通过使用 dynamic 关键字,可以创建和动态类型语言(例如 JavaScript)类似的构造。By using the dynamic keyword, you can create constructs similar to dynamically typed languages like JavaScript. 可以创建 dynamic x = "a string" 再向它添加六个,然后让运行时理清下一步操作。You can create a dynamic x = "a string" and then add six to it, leaving it up to the runtime to sort out what should happen next.

动态绑定存在出错的可能性,不过同时也为你提供了强大的语言功能。Dynamic binding gives you the potential for errors but also great power within the language.

C# 5.0 版C# version 5.0

C# 版本 5.0 随 Visual Studio 2012 一起发布,是该语言有针对性的一个版本。C# version 5.0, released with Visual Studio 2012, was a focused version of the language. 对此版本中所做的几乎所有工作都归入另一个突破性语言概念:适用于异步编程的 asyncawait 模型。Nearly all of the effort for that version went into another groundbreaking language concept: the async and await model for asynchronous programming. 下面是主要功能列表:Here is the major features list:

另请参阅See Also

调用方信息特性让你可以轻松检索上下文的信息,不需要采用大量样本反射代码。The caller info attribute lets you easily retrieve information about the context in which you're running without resorting to a ton of boilerplate reflection code. 这在诊断和日志记录任务中也很有用。It has many uses in diagnostics and logging tasks.

但是 asyncawait 才是此版本真正的主角。But async and await are the real stars of this release. C# 在 2012 年推出这些功能时,将异步引入语言作为最重要的组成部分,另现状大为改观。When these features came out in 2012, C# changed the game again by baking asynchrony into the language as a first-class participant. 如果你以前处理过冗长的运行操作以及实现回调的 Web,应该会爱上这项语言功能。If you've ever dealt with long running operations and the implementation of webs of callbacks, you probably loved this language feature.

C# 6.0 版C# version 6.0

C# 在 3.0 版和 5.0 版对面向对象的语言添加了主要的新功能。With versions 3.0 and 5.0, C# had added major new features in an object-oriented language. 版本 6.0 随 Visual Studio 2015 一起发布,通过该版本,它不再推出主导性的杀手锏,而是发布了很多使得 C# 编程更有效率的小功能。With version 6.0, released with Visual Studio 2015, it would go away from doing a dominant killer feature and instead release many smaller features that made C# programming more productive. 以下介绍了部分功能:Here are some of them:

其他新功能包括:Other new features include:

  • Catch/Finally 块中的 AwaitAwait in catch/finally blocks
  • 仅限 getter 属性的默认值Default values for getter-only properties

这些功能每一个都很有趣。Each of these features is interesting in its own right. 但从整体来看,可以发现一个有趣的模式。But if you look at them altogether, you see an interesting pattern. 在此版本中,C# 消除语言样本,让代码更简洁且更具可读性。In this version, C# eliminated language boilerplate to make code more terse and readable. 所以对喜欢简洁代码的用户来说,此语言版本非常成功。So for fans of clean, simple code, this language version was a huge win.

除了发布此版本,他们还做了另一件事,虽然这件事本身与传统的语言功能无关。They did one other thing along with this version, though it's not a traditional language feature in itself. 他们发布了 Roslyn 编译器即服务They released Roslyn the compiler as a service. C# 编译器现在是用 C# 编写的,你可以使用编译器作为编程工作的一部分。The C# compiler is now written in C#, and you can use the compiler as part of your programming efforts.

C# 7.0 版C# version 7.0

C# 7.0 版已与 Visual Studio 2017 一起发布。C# version 7.0 was released with Visual Studio 2017. 虽然该版本继承和发展了 C# 6.0,但不包含编译器即服务。This version has some evolutionary and cool stuff in the vein of C# 6.0, but without the compiler as a service. 以下介绍了部分新增功能:Here are some of the new features:

其他功能包括:Other features included:

这些都为开发者提供了很棒的新功能,帮助编写比以往任何时候都简洁的代码。All of these features offer cool new capabilities for developers and the opportunity to write even cleaner code than ever. 重点是缩减了使用 out 关键字的变量声明,并通过元组实现了多个返回值。A highlight is condensing the declaration of variables to use with the out keyword and by allowing multiple return values via tuple.

但 C# 的用途更加广泛了。But C# is being put to ever broader use. .NET Core 现在面向所有操作系统,着眼于云和可移植性。.NET Core now targets any operating system and has its eyes firmly on the cloud and on portability. 语言设计者除了推出新功能外,也会在这些新功能方面付出时间和精力。These new capabilities certainly occupy the language designers' thoughts and time, in addition to coming up with new features.

C# 7.1 版C# version 7.1

C# 已开始随 C# 7.1 发布单点发行 。C# started releasing point releases with C# 7.1. 此版本增加了语言版本选择配置元素、三个新的语言功能和新的编译器行为。This version added the language version selection configuration element, three new language features, and new compiler behavior.

此版本中新增的语言功能包括:The new language features in this release are:

最后,编译器有 -refout-refonly 两个选项,可用于控制引用程序集生成Finally, the compiler has two options -refout and -refonly that control reference assembly generation.

C# 7.2 版C# version 7.2

C# 7.2 版添加了几个小型语言功能:C# 7.2 added several small language features:

C# 7.3 版C# version 7.3

C# 7.3 版本有两个主要主题。There are two main themes to the C# 7.3 release. 第一个主题提供使安全代码的性能与不安全代码的性能一样好的功能。One theme provides features that enable safe code to be as performant as unsafe code. 第二个主题提供对现有功能的增量改进。The second theme provides incremental improvements to existing features. 此外,在此版本中添加了新的编译器选项。In addition, new compiler options were added in this release.

以下新增功能支持使安全代码获得更好的性能的主题:The following new features support the theme of better performance for safe code:

对现有功能进行了以下增强:The following enhancements were made to existing features:

新的编译器选项为:The new compiler options are:

C# 8.0 版C# version 8.0

C# 8.0 版是专门面向 .NET C# Core 的第一个主要 C# 版本。C# 8.0 is the first major C# release that specifically targets .NET Core. 一些功能依赖于新的 CLR 功能,而其他功能依赖于仅在 .NET Core 中添加的库类型。Some features rely on new CLR capabilities, others on library types added only in .NET Core. C# 8.0 向 C# 语言添加了以下功能和增强功能:C# 8.0 adds the following features and enhancements to the C# language:

默认接口成员需要 CLR 中的增强功能。Default interface members require enhancements in the CLR. 这些功能已添加到 .NET Core 3.0 的 CLR 中。Those features were added in the CLR for .NET Core 3.0. 范围和索引以及异步流需要 .NET Core 3.0 库中的新类型。Ranges and indexes, and asynchronous streams require new types in the .NET Core 3.0 libraries. 在编译器中实现时,可为 null 的引用类型在批注库时更有用,因为它可以提供有关参数和返回值的 null 状态的语义信息。Nullable reference types, while implemented in the compiler, is much more useful when libraries are annotated to provide semantic information regarding the null state of arguments and return values. 这些批注将添加到 .NET Core 库中。Those annotations are being added in the .NET Core libraries.

文章最初发布在 NDepend 博客上 ,由 Erik Dietrich 和 Patrick Smacchia 提供Article originally published on the NDepend blog, courtesy of Erik Dietrich and Patrick Smacchia.