曆史版本
C#作為微軟2000年以後.NET平台開發的當家語言,發展至今具有17年的曆史,語言本身具有豐富的特性,微軟對其更新支援也十分支援。微軟将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 Feature Status.md
語言版本 | 釋出時間 | .NET Framework要求 | Visual Studio版本 |
---|---|---|---|
C# 1.0 | 2002.1 | .NET Framework 1.0 | Visual Studio .NET 2002 |
C# 1.1\1.2 | 2003.4 | .NET Framework 1.1 | Visual Studio .NET 2003 |
C# 2.0 | 2005.11 | .NET Framework 2.0 | Visual Studio 2005 |
C# 3.0 | 2007.11 | .NET Framework 2.0\3.0\3.5 | Visual Studio 2008 |
C# 4.0 | 2010.4 | .NET Framework 4.0 | Visual Studio 2010 |
C# 5.0 | 2012.8 | .NET Framework 4.5 | Visual Studio 2012\2013 |
C# 6.0 | 2015.7 | .NET Framework 4.6 | Visual Studio 2015 |
C# 7.0 | 2017.3 | .NET Framework 4.6.2 | Visual Studio 2017 |
C# 7.1 | 2017.6 | .NET Framework 4.7 | Visual Studio 2017 v15.3預覽版 |
C# 7.2 | 2017.11 | .NET Framework 4.7.1 | Visual Studio 2017 v15.5 |
C# 7.3 | 2018.5 | .NET Framework 4.7.2 | Visual Studion 2017 v15.7 |
C# 8.0 | 待釋出 |
C# 1.0 特性
第1個版本,程式設計語言最基礎的特性。
- Classes:面向對象特性,支援類類型
- Structs:結構
- Interfaces:接口
- Events:事件
- Properties:屬性,類的成員,提供通路字段的靈活方法
- Delegates:委托,一種引用類型,表示對具有特定參數清單和傳回類型的方法的引用
- Expressions,Statements,Operators:表達式、語句、操作符
- Attributes:特性,為程式代碼添加中繼資料或聲明性資訊,運作時,通過反射可以通路特性資訊
- Literals:字面值(或了解為常量值),差別常量,常量是和變量相對的
C# 2特性 (VS 2005)
- 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# 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 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# 7.1 特征 (Visual Studio 2017 version 15.3)
- Async main:在main方法用async方式
- Default expressions:引入新的字面值default
- Reference assemblies:
- Inferred tuple element names:
- Pattern-matching with generics:
C# 7.2 特征 (Visual Studio 2017 version 15.5)
- Span and ref-like types
- In parameters and readonly references
- Ref conditional
- Non-trailing named arguments
- Private protected accessibility
- Digit separator after base specifier
C# 7.3 (Visual Studio 2017 version 15.7)
- System.Enum, System.Delegate and unmanaged constraints.
- Ref local re-assignment: Ref locals and ref parameters can now be reassigned with the ref assignment operator (= ref).
- Stackalloc initializers: Stack-allocated arrays can now be initialized, e.g. Span x = stackalloc[] { 1, 2, 3 };.
- Indexing movable fixed buffers: Fixed buffers can be indexed into without first being pinned.
- Custom fixed statement: Types that implement a suitable GetPinnableReference can be used in a fixed statement.
- Improved overload candidates: Some overload resolution candidates can be ruled out early, thus reducing ambiguities.
- Expression variables in initializers and queries: Expression variables like out var and pattern variables are allowed in field initializers, constructor initializers and LINQ queries.
- Tuple comparison: Tuples can now be compared with == and !=.
- Attributes on backing fields: Allows [field: …] attributes on an auto-implemented property to target its backing field.