『C#』语言版本历史

本文最后更新于 2019年 5月 6号 凌晨 2点整,并同步发布于 :


『C#』语言版本历史

各个 C# 版本的主要特性、发布日期和发布方式

C# 8.0

待发布

  • Default Interface Methods: 缺省接口实现

  • More patterns in more places: 在更多位置中使用更多模式

    • 模式匹配增强功能:
      • Switch 表达式
      • 属性模式
      • 元组模式
      • 位置模式
  • using declarations: using 声明

  • Static local functions: 静态本地函数

  • Disposable ref structs:可处置的 ref 结构

  • Nullable reference types: 可为空引用类型

  • Asynchronous streams: 异步流

  • Indices and ranges: 索引和范围

C# 7.3

2018 年 5 月随 Visual Studio 2017 v15.7 发布

  • Enabling more efficient safe code : 启用更高效的安全代码
    • 索引 fixed 字段不需要进行固定
    • 可以重新赋值的ref局部变量
    • stackalloc 数组支持初始值设定项
    • 更多类型支持 fixed 语句
    • 增强的泛型约束(支持EnumDelegate约束)
  • Make existing features: 提升了现有功能
    • 元组支持==!=
    • in 方法重载解析决胜属性
    • 扩展out 语法
  • New compiler options: 新的编译器选项(-publicsign-pathmap)

C# 7.2

2017 年 11 月随 Visual Studio 2017 v15.5 发布

  • Safe efficient code enhancements: 安全高效的代码的增强功能
    • 针对实参的 in 修饰符,指定形参通过引用传递,但不通过调用方法修改。 将 in 修饰符添加到参数是源兼容的更改。
    • 针对方法返回的 ref readonly 修饰符,指示方法通过引用返回其值,但不允许写入该对象。 如果向某个值赋予返回值,则添加 ref readonly 修饰符是源兼容的更改。 将 readonly 修饰符添加到现有的 ref 返回语句是不兼容的更改。 它要求调用方更新 ref 本地变量的声明以包含 readonly 修饰符。
    • readonly struct 声明,指示结构不可变,且应作为 in 参数传递到其成员方法。 将 readonly 修饰符添加到现有的结构声明是二进制兼容的更改。
    • ref struct 声明,指示结构类型直接访问托管的内存,且必须始终分配有堆栈。 将 ref 修饰符添加到现有 struct 声明是不兼容的更改。 ref struct 不能是类的成员,也不能用于可能在堆上分配的其他位置。
  • Non-trailing named arguments: 非尾随命名参数
  • Leading underscores in numeric literals: 数值文字中的前导下划线
  • private protected access modifier: private protected 访问修饰符
  • Conditional ref expressions: 条件 ref 表达式

C# 7.1

2017 年 8 月随 Visual Studio 2017 v15.3 发布

  • Async main:异步Main方法

  • Default expressions:default 表达式

  • Reference assemblies:引用程序集生成

  • Inferred tuple element names:推断元组元素名称

  • Pattern-matching with generics:

C# 7.0

2017 年 3 月随 Visual Studio 2017 和 .NET Framework 4.7 发布

  • Out variables:out变量直接声明,例如可以out in parameter

  • Pattern matching:模式匹配

  • Tuples:元组

  • Deconstruction:元组解析

  • Discards:没有命名的变量,只是占位,后面代码不需要使用其值

  • Local Functions:本地函数

  • Binary Literals:二进制字面量

  • Digit Separators:数字分隔符

  • Ref returns and locals:引用返回值和局部变量

  • Generalized async return types:async中使用泛型返回类型

  • More expression-bodied members:允许构造器、解析器、属性可以使用表达式作为body

  • Throw expressions:Throw可以在表达式中使用

C# 6.0

2015 年 7 月随 Visual Studio 2015 和 .NET Framework 4.6 发布

  • Compiler-as-a-service (Roslyn)

  • Import of static type members into namespace:支持仅导入类中的静态成员

  • Exception filters:异常过滤器

  • Await in catch/finally blocks:支持在catch/finally语句块使用await语句

  • Auto property initializers:自动属性初始化

  • Default values for getter-only properties:设置只读属性的默认值

  • Expression-bodied members:支持以表达式为主体的成员方法和只读属性

  • Null propagator (null-conditional operator, succinct null checking):Null条件操作符

  • String interpolation:字符串插值,产生特定格式字符串的新方法

  • nameof operator:nameof操作符,返回方法、属性、变量的名称

  • Dictionary initializer:字典初始化

C# 5.0

2012 年 8 月随 Visual Studio 2012 和 .NET Framework 4.5 发布

  • Asynchronous methods:异步方法

  • Caller info attributes:调用方信息特性,调用时访问调用者的信息

C# 4.0

2010 年 4 月随 Visual Studio 2010 和 .NET Framework 4.0 发布

  • Dynamic binding:动态绑定

  • Named and optional arguments:命名参数和可选参数

  • Generic co- and contravariance:泛型的协变和逆变

  • Embedded interop types (“NoPIA”):开启嵌入类型信息,增加引用COM组件程序的中立性

C# 3.0

2007 年 11 月随 Visual Studio 2008 和 .NET Framework 3.5 发布

  • Implicitly typed local variables:隐式类型本地变量

  • Object and collection initializers:对象和集合初始化器

  • Auto-Implemented properties:自动属性,自动生成属性方法,声明更简洁

  • Anonymous types:匿名类型

  • Extension methods:扩展方法

  • Query expressions:查询表达式

  • Lambda expression:Lambda表达式

  • Expression trees:表达式树,以树形数据结构表示代码,是一种新数据类型

  • Partial methods:部分方法

C# 2.0

2005 年 11 月随 Visual Studio 2005 和 .NET Framework 3.0 发布

  • Generics:泛型

  • Partial types:分部类型,可以将类、结构、接口等类型定义拆分到多个文件中

  • Anonymous methods:匿名方法

  • Iterators:迭代器

  • Nullable types:可以为Null的类型,该类可以是其它值或者null

  • Getter/setter separate accessibility:属性访问控制

  • Method group conversions (delegates):方法组转换,可以将声明委托代表一组方法,隐式调用

  • Co- and Contra-variance for delegates and interfaces:委托、接口的协变和逆变

  • Static classes:静态类

  • Delegate inference:委托推断,允许将方法名直接赋给委托变量\

C# 1.2

2003 年 4 月随 Visual Studio 2003 和 .NET Framework 1.1 发布

  • 当 IEnumerator 实现 IDisposable 时,foreach 循环中生成的代码会在 IEnumerator 上调用 Dispose。

C# 1.0

2002 年 1 月随 Visual Studio 2002 和 .NET Framework 1.0 发布

  • Classes :面向对象特性,支持类类型

  • Structs:结构

  • Interfaces:接口

  • Events:事件

  • Properties:属性,类的成员,提供访问字段的灵活方法

  • Delegates:委托,一种引用类型,表示对具有特定参数列表和返回类型的方法的引用

  • Expressions,Statements,Operators:表达式、语句、操作符

  • Attributes:特性,为程序代码添加元数据或声明性信息,运行时,通过反射可以访问特性信息

  • Literals:字面值(或理解为常量值),区别常量,常量是和变量相对的


—END—