C#語言歷史版本特性(C# 1.0到C# 8.0彙總)

C#作為微軟2000年以後.NET平臺開發的當家語言,語言本身具有豐富的特性,微軟對其更新支持也十分支持。微軟將C#提交給標準組織ECMA,C# 5.0目前是ECMA發佈的最新規範,C# 6.0還是草案階段,C# 7.1是微軟當前提供的最新規範。

這裡僅僅列個提綱,由於C# 5.0是具有ECMA標準規範的版本,所以選擇C# 5.0作為主要版本學習,並專題學習C# 6.0,7.0版本新特性。

C#語言規範GitHub庫參見:https://github.com/dotnet/csharplang

C#語言路線圖及開發中的特性參見:

https://github.com/dotnet/roslyn/blob/master/docs/Language%20Feature%20Status.md

C# 1.0 特性

Classes:面向對象特性,支持類類型Structs:結構Interfaces:接口Events:事件Properties:屬性,類的成員,提供訪問字段的靈活方法Delegates:委託,一種引用類型,表示對具有特定參數列表和返回類型的方法的引用Expressions,Statements,Operators:表達式、語句、操作符Attributes:特性,為程序代碼添加元數據或聲明性信息,運行時,通過反射可以訪問特性信息Literals:字面值(或理解為常量值),區別常量,常量是和變量相對的

C# 2特性 (VS 2005)

Generics:泛型Partial types:分部類型,可以將類、結構、接口等類型定義拆分到多個文件中Anonymous methods:匿名方法Iterators:迭代器Nullable types:可以為Null的類型,該類可以是其它值或者nullGetter/setter separate accessibility:屬性訪問控制Method group conversions (delegates):方法組轉換,可以將聲明委託代表一組方法,隱式調用Co- and Contra-variance for delegates and interfaces:委託、接口的協變和逆變Static classes:靜態類Delegate inference:委託推斷,允許將方法名直接賦給委託變量

C# 3特性 (VS 2008)

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# 4特性 (VS 2010)

Dynamic binding:動態綁定Named and optional arguments:命名參數和可選參數Generic co- and contravariance:泛型的協變和逆變Embedded interop types (“NoPIA”):開啟嵌入類型信息,增加引用COM組件程序的中立性

C# 5特性 (VS 2012)

Asynchronous methods:異步方法Caller info attributes:調用方信息特性,調用時訪問調用者的信息

C# 6特徵 (VS 2015)

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# 7 特徵 (Visual Studio 2017)

Out variables:out變量直接聲明,例如可以out in parameterPattern matching:模式匹配,根據對象類型或者其它屬性實現方法派發Tuples:元組Deconstruction:元組解析Discards:沒有命名的變量,只是佔位,後面代碼不需要使用其值Local Functions:局部函數Binary Literals:二進制字面量Digit Separators:數字分隔符Ref returns and locals:引用返回值和局部變量Generalized async return types:async中使用泛型返回類型More expression-bodied members:允許構造器、解析器、屬性可以使用表達式作為bodyThrow expressions:Throw可以在表達式中使用

C# 7.1 特徵 (Visual Studio 2017 version 15.3)

Async main:在main方法用async方式Default expressions:引入新的字面值defaultReference assemblies:Inferred tuple element names:Pattern-matching with generics:

C# 8.0 特徵 (Visual Studio 2017 version 15.7)

Default Interface Methods 缺省接口實現Nullable reference type NullableReferenceTypes 非空和可控的數據類型Recursive patterns 遞歸模式Async streams 異步數據流Caller expression attribute 調用方法表達式屬性Target-typed newGeneric attributes 通用屬性RangesDefault in deconstructionRelax ordering of ref and partial modifiers