C# 語言特性發展史


C# 語言特性發展史

Intro

本文主要總結介紹C# 每個版本帶來的不同的語言特性。

C#,讀作C Sharp,是微軟推出的一種基於.NET平台的、面向對象的高級編程語言。是微軟公司在2000年發布的一種新的編程語言,主要由安德斯·海爾斯伯格(Anders Hejlsberg)主持開發,它是第一個面向組件的編程語言,其源碼會編譯成msil再運行。它借鑒了Delphi的一個特點,與COM(組件對象模型)是直接集成的,並且新增了許多功能及語法糖。

C# 1.x

自 2000 年 C#1.0 發布之后,微軟在2003年4月又發布了 C# 1.1 主要是修復BUG,這里統稱為1.x

  • 面向對象
  • 內存自動回收,GC
  • 屬性
  • 反射

C# 2

  • 泛型
  • 分部類
  • 靜態類型
  • 迭代器(yield return)
  • 匿名方法(lambda 表達式)
  • 可空類型
  • 委托的協變逆變
  • 屬性訪問器可以被單獨設置訪問級別
  • ??表達式

C# 3

  • Linq
  • 類型初始化器
  • 集合初始化器
  • 匿名類型
  • 局部變量類型推斷(var)
  • Lambda 表達式
  • 自動屬性
  • 擴展方法
  • 分部方法
  • 表達式樹(Expression Tree)

C# 4

  • 動態編程(dynamic
  • 具名參數與可選參數
  • 泛型的協變和逆變
  • TPL任務並行庫,基於Task的異步編程

C# 5

  • 異步編程(async&await)
  • 調用方信息特性(CallerMemberName&CallerFilePath&CallerLineNumber)

C# 6

  • 靜態導入(using static

  • 異常過濾器(when(ex.ExceptionCode == 111)

  • 屬性初始化器(public int PageIndex {get;} = 1;

  • 字典初始化器

    private Dictionary<int, string> webErrors = new Dictionary<int, string>
    {
        [404] = "Page not Found",
        [302] = "Page moved, but left a forwarding address.",
        [500] = "The web server can't come out to play today."
    };
    
  • 字符串插值($"abc{123}def"

  • nameof 運算符

  • null判斷傳播運算符(a?SomeProperty?.Abc??"abcd"

  • 表達式體方法(int Add(int a,int b) => a+b;

  • catch和finally子句中支持 await

  • 只讀自動屬性(public int Count {get;}

C# 7

  • out 變量(int.TryParse("123", out var num);

  • 優化元祖支持,支持變量名((int max,int min) top = (3,1);

  • 廢棄變量(if(int.TryParse("123",out _))

  • 模式匹配(if(abc is int num)switch...case支持模式匹配)

  • ref local and ref return(ref return 123;

  • 本地方法(局部方法)

  • 更多的支持表達式體方法(增加支持屬性和索引器上實現構造函數、終結器以及 get 和 set 訪問器)

  • throw表達式

  • 數字文本語法改進

    誤讀的數值常量可能使第一次閱讀代碼時更難理解。 當這些數字被用作位掩碼或其他符號而非數字值時,通常會發生這種情況。 C# 7.0 包括兩項新功能,使得更容易以最可讀的方式寫入數字來用於預期用途:二進制文本和數字分隔符

    public const int Sixteen =   0b0001_0000;
    public const int ThirtyTwo = 0b0010_0000;
    public const int SixtyFour = 0b0100_0000;
    public const int OneHundredTwentyEight = 0b1000_0000;
    
    public const long BillionsAndBillions = 100_000_000_000;
    
    public const double AvogadroConstant = 6.022_140_857_747_474e23;
    public const decimal GoldenRatio = 1.618_033_988_749_894_848_204_586_834_365_638_117_720_309_179M;
    

C# 7.1

  • 異步Main方法(async Main()

  • 默認常值表達式(Func<string, bool> whereClause = default;

  • 推斷元組元素名稱

    // C# 7
    int count = 5;
    string label = "Colors used in the map";
    var pair = (count: count, label: label);
    
    // C# 7.1
    int count = 5;
    string label = "Colors used in the map";
    var pair = (count, label); // element names are "count" and "label"
    

C# 7.2

  • 語言版本選擇(支持在項目中指定要使用的C#版本)

  • 數值文字中的前導下划線

    C# 7.0 中實現了對數字分隔符的支持,但這不允許文字值的第一個字符是 _。 十六進制文本和二進制文件現可以 _ 開頭。

    int binaryValue = 0b_0101_0101;
    
  • private protected 訪問修飾符(可通過包含同一程序集中聲明的類或派生類來訪問成員)

C# 7.3(Preview)

  • 元組支持相等性比較

  • 新的泛型約束(Enum,Delegate,unmanaged)

  • Ref 局部變量重新分配(Ref 局部變量和 ref 參數現在可通過 ref 分配運算符重新分配 = ref

  • Stackalloc 初始化表達式

    Span<int> x = stackalloc[] { 1, 2, 3 };
    
  • 初始化表達式和查詢中的表達式變量

  • 支持字段的特性

    允許自動實現的屬性上的 [field: …] 特性定位其支持字段

    // C# 7.3
    [Serializable]
    public class Foo {
      [field: NonSerialized]
      public string MySecret { get; set; }
    }
    
    // above code equals the code below
    [Serializable]
    public class Foo {
      [NonSerialized]
      private string MySecret_backingField;
    
      public string MySecret {
        get { return MySecret_backingField; }
        set { MySecret_backingField = value; }
      }
    }
    

C# 8(Preview)

  • 可空引用類型(引用類型默認不可為空,如果需要為可空則需要顯示聲明string? abc = null;

Reference


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM