# C#

**C#**（读作“C sharp”）是一门由微软（Microsoft）内部[安德斯·海尔斯伯格](https://www.edgepedia.org/zh/anders-hejlsberg)（Anders Hejlsberg）领导的团队设计的编程语言，于2000年首次发布。它编译为中间语言（Intermediate Language，IL），在.NET平台的[公共语言运行时](https://www.edgepedia.org/zh/common-language-runtime)（Common Language Runtime，CLR）上运行，其规范由国际唱片业协会（Ecma International）标准化为ECMA-334，第7版日期为2023年12月。<sup>[1](https://ecma-international.org/publications-and-standards/standards/ecma-334/)</sup><sup> • </sup><sup>[2](https://technav.ieee.org/topic/c-sharp-languages/)</sup><sup> • </sup><sup>[3](https://ecma-international.org/wp-content/uploads/ECMA-334_1st_edition_december_2001.pdf)</sup>

| 关键事实 | 详情 |
|---|---|
| 首次发布 | 2000年6月26日宣布；微软于2000年7月随.NET Framework计划发布首个实现<sup>[4](https://news.microsoft.com/source/2000/06/26/microsoft-introduces-highly-productive-net-programming-language-c/)</sup><sup> • </sup><sup>[3](https://ecma-international.org/wp-content/uploads/ECMA-334_1st_edition_december_2001.pdf)</sup> |
| 主要发明人 | 安德斯·海尔斯伯格、斯科特·威尔塔穆思（Scott Wiltamuth）、彼得·戈尔德（Peter Golde）<sup>[3](https://ecma-international.org/wp-content/uploads/ECMA-334_1st_edition_december_2001.pdf)</sup> |
| 标准化 | 自2001年12月起为ECMA-334；C# 7对应ECMA-334:2023和ISO/IEC 20619:2023<sup>[3](https://ecma-international.org/wp-content/uploads/ECMA-334_1st_edition_december_2001.pdf)</sup><sup> • </sup><sup>[1](https://ecma-international.org/publications-and-standards/standards/ecma-334/)</sup> |
| 执行模型 | 源代码编译为IL，CLR可将其即时编译为机器码<sup>[5](https://link.springer.com/book/10.1007/978-1-4302-6671-6)</sup> |
| 泛型 | 自C# 2.0起在运行时具化，对值类型进行JIT特化<sup>[2](https://technav.ieee.org/topic/c-sharp-languages/)</sup><sup> • </sup><sup>[6](https://newsletter.techworld-with-milan.com/p/why-csharp)</sup> |
| 当前版本 | C# 14，2025年11月发布<sup>[7](https://learn.microsoft.com/en-us/dotnet/csharp/whats-new/csharp-version-history)</sup> |
| 流行度 | 2026年10月TIOBE指数排名第5，占3.79%；微软称其为GitHub项目使用的前5大语言之一<sup>[8](https://www.tiobe.com/tiobe-index/)</sup><sup> • </sup><sup>[9](https://dotnet.microsoft.com/en-us/languages/csharp)</sup> |

## 历史与标准化

微软于2000年6月26日公开宣布C#，称其为“一门现代的、面向对象的编程语言，从头构建以利用.NET平台上基于XML的Web服务的强大能力。”<sup>[4](https://news.microsoft.com/source/2000/06/26/microsoft-introduces-highly-productive-net-programming-language-c/)</sup>首个广泛分发的实现于2000年7月作为.NET Framework计划的一部分面世。<sup>[3](https://ecma-international.org/wp-content/uploads/ECMA-334_1st_edition_december_2001.pdf)</sup>

标准化很快跟进。Ecma第39技术委员会第2任务组于2000年9月成立，以制定C#标准，ECMA-334第一版于2001年12月出版。<sup>[3](https://ecma-international.org/wp-content/uploads/ECMA-334_1st_edition_december_2001.pdf)</sup>该标准的版本映射将语言版本与Ecma和ISO版本对应起来：C#版本1对应ECMA-334:2003和ISO 23270:2003，版本5对应ECMA-334:2017和ISO 23270:2018，版本7对应ECMA-334:2023，ISO/IEC 20619:2023通过引用纳入该版本。<sup>[1](https://ecma-international.org/publications-and-standards/standards/ecma-334/)</sup>运行时契约另行标准化为ECMA-335和ISO/IEC 23271。<sup>[2](https://technav.ieee.org/topic/c-sharp-languages/)</sup>

**独立实现。** 开放标准促成了[Mono](https://www.edgepedia.org/zh/mono-software)，这是一个2001年宣布的社区跨平台实现；同年11月14日，其C#编译器在Linux上运行了“Hello world”，到11月30日，所有解释器测试在Mono JIT引擎上通过。<sup>[10](https://www.mono-project.com/docs/about-mono/history/)</sup>Mono编译器于2002年1月3日实现自举。<sup>[11](https://www.mono-project.com/docs/about-mono/languages/csharp/)</sup>微软后来宣布.NET Core（包括运行时和框架库）将开源，这建立在此前C#、VB和F#托管编译器开源的基础上。<sup>[12](https://devblogs.microsoft.com/dotnet/net-core-is-open-source/)</sup>.NET如今是免费、开源的项目，作为.NET基金会项目在GitHub上维护。<sup>[13](https://learn.microsoft.com/en-us/dotnet/core/introduction)</sup>

## 语言设计与特性

泛型随C# 2.0到来，在运行时层面实现具化。<sup>[2](https://technav.ieee.org/topic/c-sharp-languages/)</sup>

**具化泛型。** 在CLR的设计中，每个对象都携带完整类型信息，包括参数化类型的类型参数，因此向不兼容的泛型实例化进行强制转换会在运行时引发InvalidCastException。<sup>[14](https://www.microsoft.com/en-us/research/wp-content/uploads/2001/01/designandimplementationofgenerics.pdf)</sup>该实现执行即时类型特化和基于表示的代码共享，早期结果显示性能几乎与手工特化代码相当。<sup>[14](https://www.microsoft.com/en-us/research/wp-content/uploads/2001/01/designandimplementationofgenerics.pdf)</sup>该设计还支持参数化类型、多态静态方法、实例方法和虚方法、F-界类型参数、在指针和值类型上实例化、多态递归以及精确运行时类型；多态定义只验证一次，不像C++模板和基于类加载器特化的Java扩展那样，每个不同的实例化都必须单独检查。<sup>[14](https://www.microsoft.com/en-us/research/wp-content/uploads/2001/01/designandimplementationofgenerics.pdf)</sup>

**主要特性里程碑。** C# 3.0增加了LINQ（语言集成查询）；C# 5.0随[Visual Studio](https://www.edgepedia.org/zh/visual-studio) 2012发布，几乎全部精力都投入到了async和await模型上。<sup>[2](https://technav.ieee.org/topic/c-sharp-languages/)</sup><sup> • </sup><sup>[7](https://learn.microsoft.com/en-us/dotnet/csharp/whats-new/csharp-version-history)</sup>C# 8.0是首个专门面向.NET Core的主要版本，增加了可空引用类型、异步流、索引与范围、switch表达式，以及需要在.NET Core 3.0中增强CLR的默认接口成员。<sup>[7](https://learn.microsoft.com/en-us/dotnet/csharp/whats-new/csharp-version-history)</sup>C# 10后来增加了记录结构体、全局using、文件作用域命名空间、nint/nuint和函数指针。<sup>[7](https://learn.microsoft.com/en-us/dotnet/csharp/whats-new/csharp-version-history)</sup>

**可空引用类型**在C# 8.0中引入。<sup>[7](https://learn.microsoft.com/en-us/dotnet/csharp/whats-new/csharp-version-history)</sup>

## 执行模型与运行时

在基于JIT的执行中，C#从源代码经过微软中间语言（Microsoft Intermediate Language，MSIL），再经即时编译为机器码，在分层的.NET抽象栈中于硬件层面执行。<sup>[5](https://link.springer.com/book/10.1007/978-1-4302-6671-6)</sup>C#编译器生成在CLR上运行的IL，CLR与F#和[Visual Basic](https://www.edgepedia.org/zh/visual-basic)等其他.NET语言共享。<sup>[6](https://newsletter.techworld-with-milan.com/p/why-csharp)</sup>对于值类型，JIT编译器为每个泛型实例化生成特化代码，因此`List<int>`和`List<double>`获得各自的优化实现，没有装箱开销。<sup>[6](https://newsletter.techworld-with-milan.com/p/why-csharp)</sup>

**内存管理。**.NET通过自调优的追踪垃圾回收器提供自动内存管理，目标是在一般情况下免手动干预，同时为极端工作负载提供配置选项。值类型和栈分配内存提供低层控制，而`List<T>`等泛型类型可以为值类型集合提供扁平、无开销的内存表示。<sup>[13](https://learn.microsoft.com/en-us/dotnet/core/introduction)</sup>

**async/await状态机。** C#编译器将async方法重写为状态机，同时保留开发者编写的方法签名。它生成一个结构体（命名如`<CopyStreamToStreamAsync>d__0`，实现`IAsyncStateMachine`），在栈上零初始化；方法的局部变量成为状态机的字段，所有代码块编译为单个`MoveNext`方法，其中包含一个运行下一个代码块的switch，反复执行而不阻塞调用线程。<sup>[15](https://devblogs.microsoft.com/dotnet/how-async-await-really-works/)</sup><sup> • </sup><sup>[16](https://tomasp.net/blog/async-compilation-internals.aspx/)</sup>如果async方法同步完成，状态机从不离开栈，因此除非方法实际等待了未完成的操作，状态机本身无需堆分配。<sup>[15](https://devblogs.microsoft.com/dotnet/how-async-await-really-works/)</sup>该特性主要由C#编译器而非.NET运行时实现。<sup>[17](https://www.oreilly.com/library/view/c-concurrency/9781633438651/Text/02.html)</sup>

**运行时格局。** 目前三种实现并存：.NET（Core），为云时代重新构思的现代跨平台开源实现；.NET Framework，最初的Windows实现，活跃支持但处于维护状态；以及Mono，.NET Framework最初的社区开源实现。<sup>[13](https://learn.microsoft.com/en-us/dotnet/core/introduction)</sup>

## 数据一览

- [TIOBE指数](https://www.edgepedia.org/zh/tiobe-index)，2026年10月：C#排名第5，占3.79%，同比下降3.15个百分点，落后于Python（19.23%）、C++（8.87%）和Java（8.41%）。<sup>[8](https://www.tiobe.com/tiobe-index/)</sup>
- GitHub：微软称C#是GitHub项目使用的前5大语言之一，并且一直在Stack Overflow开发者调查中最受喜爱的语言之列，被媒体、金融、医疗和游戏行业的数十万家公司使用。<sup>[9](https://dotnet.microsoft.com/en-us/languages/csharp)</sup>
- 职位需求：C#在DevJobsScanner的“2024年最需要的8大编程语言”中排名第4，基于21个月内分析的1200万个开发者职位。<sup>[6](https://newsletter.techworld-with-milan.com/p/why-csharp)</sup>
- 包：NuGet生态拥有超过40万个包。<sup>[18](https://testdouble.com/insights/c-and-net-tools-and-libraries-for-the-modern-developer)</sup>
- 基准测试：在基准测试游戏套件的一个测试中，openjdk 21上的Java以460毫秒完成，峰值内存574.1 MB；dotnet 9.0.303（AOT）上的C#耗时940毫秒，内存167.7 MB；Mono 6.12.0下的C#耗时1675毫秒。<sup>[19](https://programming-language-benchmarks.vercel.app/csharp-vs-java)</sup>在一个子串微基准测试中，Rust用时3毫秒，Java用时1.4秒，使用Span的C#用时1.4秒，[JavaScript](https://www.edgepedia.org/zh/javascript)用时2.1秒；作者将C#和Go较慢的结果归因于托管内存。<sup>[20](https://blogs.oracle.com/timesten/executable-performance)</sup>在同一台机器上，C#、C++、Java和Go之间的基准测试结果也会随电源设置、温度、后台活动和输入数据偏差而波动，因此标准库实现非常重要。<sup>[21](https://comcomponent.com/en/blog/2026/03/17/000-language-benchmark-csharp-cpp-java-go/)</sup>

## 与Java、C++及其他C族语言的比较

**与Java对比。** C#和Java都使用垃圾回收和JIT编译，性能特征相似，但Java拥有庞大的开放生态，有多个竞争实现（OpenJDK、Oracle JDK），而C#的生态统一在.NET之下，Mono/Xamarin现已与核心统一。C#的特性如属性、运算符重载、LINQ和async/await可以比Java的对应物产生更简洁的代码。<sup>[6](https://newsletter.techworld-with-milan.com/p/why-csharp)</sup>泛型的对比是结构性的：CLR泛型是具化的，因此C#保留精确的运行时类型，并避免值类型泛型集合的装箱，而基于类加载器特化的Java扩展要求每个不同的实例化单独检查。<sup>[14](https://www.microsoft.com/en-us/research/wp-content/uploads/2001/01/designandimplementationofgenerics.pdf)</sup><sup> • </sup><sup>[2](https://technav.ieee.org/topic/c-sharp-languages/)</sup>

**与C++对比。** C++模板支持C#和Java泛型的严格超集；C++支持`vector<AnyType>`，无论`AnyType`是什么类型都具有真正的连续存储，而C#泛型集合也可以为值类型集合提供扁平、无开销的表示。<sup>[22](https://isocpp.org/wiki/faq/csharp-java)</sup>在内存管理方面，C#运行在追踪垃圾回收器之下。<sup>[13](https://learn.microsoft.com/en-us/dotnet/core/introduction)</sup>

上述基准测试套件显示Java在一个测试中领先于C# AOT（460毫秒对940毫秒），<sup>[19](https://programming-language-benchmarks.vercel.app/csharp-vs-java)</sup>而专业分析将两种语言的性能描述为相似；<sup>[6](https://newsletter.techworld-with-milan.com/p/why-csharp)</sup>两者对于不同工作负载都可能是真的，而基准测试方法学来源警告结果会随机器和输入条件波动。<sup>[21](https://comcomponent.com/en/blog/2026/03/17/000-language-benchmark-csharp-cpp-java-go/)</sup>与Rust对比，子串微基准测试中3毫秒对1.4秒的差距（归因于托管内存）说明了C#在内存访问受限的工作中为其运行时付出的代价。<sup>[20](https://blogs.oracle.com/timesten/executable-performance)</sup>

## 生态系统与应用

C#的独立实现历史塑造了它的采用。Mono对Ecma标准的实现是C#成为[Unity](https://www.edgepedia.org/zh/unity-game-engine)引擎脚本语言的原因，而Unity和[Godot](https://www.edgepedia.org/zh/godot-game-engine)这两个广泛使用的游戏引擎都完全支持C#。<sup>[2](https://technav.ieee.org/topic/c-sharp-languages/)</sup><sup> • </sup><sup>[18](https://testdouble.com/insights/c-and-net-tools-and-libraries-for-the-modern-developer)</sup>微软于2016年收购Xamarin，将Mono的核心开发者带入公司。<sup>[18](https://testdouble.com/insights/c-and-net-tools-and-libraries-for-the-modern-developer)</sup>除游戏外，微软报告其被用于媒体、金融、医疗和游戏行业。<sup>[9](https://dotnet.microsoft.com/en-us/languages/csharp)</sup>

## 近期发展

语言版本现在随.NET发布每年更新。**C# 12**（2023年11月）增加了适用于任何类或结构体的主构造函数、带展开元素的集合表达式、内联数组、可选lambda参数和任意类型别名using指令。<sup>[7](https://learn.microsoft.com/en-us/dotnet/csharp/whats-new/csharp-version-history)</sup>**C# 13**（2024年11月）将`params`集合从数组类型扩展到任何可识别的集合类型（包括Span），增加了新的`System.Threading.Lock`类型、`\e`转义序列、实现接口的ref结构体和分部属性，并预览了字段支持的属性。<sup>[7](https://learn.microsoft.com/en-us/dotnet/csharp/whats-new/csharp-version-history)</sup>**C# 14**（2025年11月）增加了扩展成员、空条件赋值、字段支持的属性、分部事件和构造函数、用户定义的复合赋值运算符、用于未绑定泛型的`nameof`，以及一等Span/ReadOnlySpan支持。<sup>[7](https://learn.microsoft.com/en-us/dotnet/csharp/whats-new/csharp-version-history)</sup>

这些版本的方向是一致的：与基于span的低层内存工作更紧密集成，通过主构造函数和字段支持的属性减少样板构造，扩展类型系统的覆盖范围，同时运行时整合到开源、跨平台的.NET上。<sup>[7](https://learn.microsoft.com/en-us/dotnet/csharp/whats-new/csharp-version-history)</sup><sup> • </sup><sup>[13](https://learn.microsoft.com/en-us/dotnet/core/introduction)</sup>

## References

1. [ECMA-334, Ecma International](https://ecma-international.org/publications-and-standards/standards/ecma-334/)
2. [C# languages, IEEE Technology Navigator](https://technav.ieee.org/topic/c-sharp-languages/)
3. [ECMA-334, 1st edition, December 2001, Ecma International](https://ecma-international.org/wp-content/uploads/ECMA-334_1st_edition_december_2001.pdf)
4. [Microsoft Introduces Highly Productive .NET Programming Language: C#, Microsoft press release, June 26, 2000](https://news.microsoft.com/source/2000/06/26/microsoft-introduces-highly-productive-net-programming-language-c/)
5. [C# Deconstructed, Apress/Springer](https://link.springer.com/book/10.1007/978-1-4302-6671-6)
6. [Why C#, Techworld with Milan newsletter](https://newsletter.techworld-with-milan.com/p/why-csharp)
7. [The history of C#, Microsoft Learn](https://learn.microsoft.com/en-us/dotnet/csharp/whats-new/csharp-version-history)
8. [TIOBE Index](https://www.tiobe.com/tiobe-index/)
9. [C# – a modern, open-source programming language, .NET](https://dotnet.microsoft.com/en-us/languages/csharp)
10. [History, Mono project](https://www.mono-project.com/docs/about-mono/history/)
11. [C# Compiler, Mono project](https://www.mono-project.com/docs/about-mono/languages/csharp/)
12. [.NET Core is Open Source, .NET Blog](https://devblogs.microsoft.com/dotnet/net-core-is-open-source/)
13. [Introduction to .NET, Microsoft Learn](https://learn.microsoft.com/en-us/dotnet/core/introduction)
14. [Design and Implementation of Generics for the .NET Common Language Runtime, Kennedy & Syme, Microsoft Research](https://www.microsoft.com/en-us/research/wp-content/uploads/2001/01/designandimplementationofgenerics.pdf)
15. [How Async/Await Really Works in C#, Stephen Toub, .NET Blog](https://devblogs.microsoft.com/dotnet/how-async-await-really-works/)
16. [Asynchronous C# and F# (III.): How does it work?, Tomas Petricek](https://tomasp.net/blog/async-compilation-internals.aspx/)
17. [C# Concurrency, Chapter 2, O'Reilly](https://www.oreilly.com/library/view/c-concurrency/9781633438651/Text/02.html)
18. [C# and .NET tools and libraries for the modern developer, TestDouble](https://testdouble.com/insights/c-and-net-tools-and-libraries-for-the-modern-developer)
19. [C# VS Java benchmarks, Benchmarks Game](https://programming-language-benchmarks.vercel.app/csharp-vs-java)
20. [Executable performance, Oracle blog](https://blogs.oracle.com/timesten/executable-performance)
21. [C# vs C++ vs Java vs Go: How to Benchmark Fairly, Component](https://comcomponent.com/en/blog/2026/03/17/000-language-benchmark-csharp-cpp-java-go/)
22. [Standard C++ FAQ: C#/Java comparisons, isocpp.org](https://isocpp.org/wiki/faq/csharp-java)

---
*Initially written Oct 15, 2026*

Topic: 技术与人造世界 › 计算技术与数字系统 › 软件与编程 › Programming languages › Individual programming languages › C-family and imperative languages

Copyright 2026 Edgepedia

License: Edgepedia Community License 1.0, https://www.edgepedia.org/edgepedia/license

Source: https://www.edgepedia.org/zh/c-epg2fs2haw

English original: https://www.edgepedia.org/c-epg2fs2haw
