你可能不知道的C#特性 - 更新到9.0

[toc]html

C# Language Design

C# Language Specification

Standard ECMA-334

  • 2002.12,包含C# 1.0,C# 1.1,C# 1.2。
  • 2006.06,包含C# 2.0。
  • 2017.12,C# 5.0版本,最新版本

Microsoft

Version Date .NET Framework CLR Visual Studio
C# 1.0 2002.01 .NET Framework 1.0 1.0 VS 2002
C# 1.1<br />C# 1.2 2003.10 .NET Framework 1.1 1.1 VS 2003
C# 2.0 2005.11 .NET Framework 2.0 2.0 VS 2005
C# 3.0 2007.11 .NET Framework 2.0<br />.NET Framework 3.0<br />.NET Framework 3.5 2.0 VS 2008<br />VS 2010
C# 4.0 2010.04 .NET Framework 4 4.0 VS 2010
C# 5.0 2012.08 .NET Framework 4.5<br />.NET Framework 4.5.1 4.0 VS 2012<br />VS 2013
C# 6.0 2015.07 .NET Framework 4.6 4.0 VS 2015
C# 7.0 2017.03 .NET Framework 4.6.2 4.0 VS 2017
C# 7.1 2017.08 .NET Framework 4.7 4.0 VS 2017 v15.3
C# 7.2 2017.11 .NET Framework 4.7.1 4.0 VS 2017 v15.5
C# 7.3 2018.05 .NET Framework 4.7.2 4.0 VS 2017 v15.7
C# 8.0 2019.10 .NET Framework 4.8 4.0 VS 2019 v16.3

C# Language Feature

C# 1.0 - Visual Studio .NET 2002

  • Classes:類,面向對象特性
  • Structs:結構
  • Interfaces:接口
  • Events:事件
  • Properties:屬性,類的成員,提供訪問字段的靈活方法。
  • Delegates:委託,一種引用類型,表示對具備特定參數列表和返回類型的方法的引用,相似於函數指針。
  • Expressions:表達式
  • Statements:語句是以分號結尾的單行代碼,也能夠是語句塊中的一系列單行語句。 語句塊括在括號 {} 中,而且能夠包含嵌套塊。
  • Attributes:特性,爲程序代碼添加元數據或聲明性信息,運行時,經過反射能夠訪問特性信息。
  • Literals:字面量,也就是不用經過構造函數來建立值

C# 1.2 - Visual Studio .NET 2003

  • Dispose in foreach:當 IEnumerator 實現 IDisposable 時,foreach 循環中生成的代碼會在 IEnumerator 上調用 Dispose。git

  • foreach over string specialization:foreach上的字符串特化。github

C# 2.0 - Visual Studio 2005

  • Generics:泛型
  • Partial types:能夠將類、結構、接口等類型定義拆分到多個文件中
  • Anonymous methods:匿名方法
  • Iterators:迭代器
  • Nullable types:可爲空的值類型
  • Getter/setter separate accessibility:屬性訪問控制
  • Method group conversions (delegates):方法組轉換,能夠將聲明委託表明一組方法,隱式調用
  • Co- and Contra-variance for delegates and interfaces:協變和逆變可以實現數組類型、委託類型參數的隱式引用轉換。 協變保留分配兼容性,逆變則與之相反。
  • Static classes:靜態類
  • Delegate inference:委託推斷,將一個方法的地址傳送給一個委託變量,編譯器會自動檢測委託變量的委託類型,而後根據委託類型建立委託實例,並把方法地址傳送給委託的構造函數。

C# 3.0 - Visual Studio 2008

C# 4.0 - Visual Studio 2010

C# 5.0 - Visual Studio 2012

  • Asynchronous methods:異步方法
  • Caller info attributes:調用方信息特性,被調用時訪問調用者的信息
  • foreach loop was changed to generates a new loop variable rather than closing over the same variable every time:foreach循環生成一組新變量而不是每次都隱藏舊變量

C# 6.0 - Visual Studio 2015

C# 7.0 - Visual Studio 2017

C# 7.1 - Visual Studio 2017 version 15.3

C# 7.2 - Visual Studio 2017 version 15.5

C# 7.3 - Visual Studio 2017 version 15.7

C# 8.0 - .NET Core 3.0 and Visual Studio 2019 version 16.3

  • Nullable reference types: express nullability intent on reference types with ?, notnull constraint and annotations attributes in APIs, the compiler will use those to try and detect possible null values being dereferenced or passed to unsuitable APIs.
  • Default interface members: interfaces can now have members with default implementations, as well as static/private/protected/internal members except for state (ie. no fields).
  • Recursive patterns: positional and property patterns allow testing deeper into an object, and switch expressions allow for testing multiple patterns and producing corresponding results in a compact fashion.
  • Async streams: await foreach and await using allow for asynchronous enumeration and disposal of IAsyncEnumerable collections and IAsyncDisposable resources, and async-iterator methods allow convenient implementation of such asynchronous streams.
  • Enhanced using: a using declaration is added with an implicit scope and using statements and declarations allow disposal of ref structs using a pattern.
  • Ranges and indexes: the i..j syntax allows constructing System.Range instances, the ^k syntax allows constructing System.Index instances, and those can be used to index/slice collections.
  • Null-coalescing assignment: ??= allows conditionally assigning when the value is null.
  • Static local functions: local functions modified with static cannot capture this or local variables, and local function parameters now shadow locals in parent scopes.
  • Unmanaged generic structs: generic struct types that only have unmanaged fields are now considered unmanaged (ie. they satisfy the unmanaged constraint).
  • Readonly members: individual members can now be marked as readonly to indicate and enforce that they do not modify instance state.
  • Stackalloc in nested contexts: stackalloc expressions are now allowed in more expression contexts.
  • Alternative interpolated verbatim strings: @$"..." strings are recognized as interpolated verbatim strings just like $@"...".
  • Obsolete on property accessors: property accessors can now be individually marked as obsolete.
  • Permit t is null on unconstrained type parameter

C# 9.0

  • Records and Pattern-based With-Expression:Records是一種輕量級的不可變類型。
  • Type Classes:Type Classes容許您在類型上添加一組操做,但不實現它。
  • Dictionary Literals:字典字面量。
  • Params Span<T>:容許用params修飾Span<T>。
  • Allow no-arg constructor and field initializers in struct declarations:結構體容許使用無參構造函數。
  • Native-Sized Number Types:原生大小的數字類型(nint,nuint,nfloat等)'n'表示native(原生),該特性容許聲明一個32位或64位的數據類型,這取決於操做系統的平臺類型。
  • Fixed Sized Buffers:固定大小的緩衝區。
  • Uft8 string literals:新的字符串類型UTF8String。
  • Base(T):解決默認接口方法中的覆蓋衝突問題

Reference

相關文章
相關標籤/搜索