C#語法索引

本文將介紹大多數(shù)C#的關(guān)鍵字和語法,并且按照C#語言版本進(jìn)行分類,便于C#使用者速查。
大部分資料來自于wikipedia,msdn及相關(guān)網(wǎng)站的鏈接。

C# 2.0

feature keyword/example remark
泛型(Generics) class Foo<T> { T obj; } 包含泛型類型/方法/字段/委托/泛型約束
部分類型 partial class Foo{ }
匿名方法 delegate(object o) { return 0; }
迭代器 IEnumerable/IEnumerator/foreach/yield
可空類型 int? a = null; Nullable<int> b = null;
屬性訪問權(quán)限 int Val { get {return _val;} private set { _val = value;}} 可以對(duì)getter/setter分別設(shè)置訪問權(quán)限
方法組轉(zhuǎn)換 void fun(){}; Action act = this.fun; 委托省略new的寫法
委托協(xié)變逆變 delegate Base Action(Derived arg); Derived fun(Base arg){}; Action act = fun;
靜態(tài)類 static class Utils {}
委托類型推導(dǎo) Action act = delegate{}

C# 3.0

feature keyword/example remark
隱式類型變量 var i = 10;
對(duì)象初始器 var a = new Point { X = 0, Y = 1 };
集合初始器 var a = new List<int> {1,2,3}; 識(shí)別Add方法
自動(dòng)屬性 int Val {get;set;}
匿名類型 var a = new { x = 0, y = 1 };
擴(kuò)展方法 static void fun(this object o){}; getObj().fun(); 依賴ExtensionAttribute
LINQ查詢 from/join/on/equals/into/let/orderby/ ascending/descending/select/group/by 編譯為對(duì)應(yīng)方法
Lambda表達(dá)式 Action act = ()=>fun();
表達(dá)式樹 Expression<Action> act = ()=>fun();
部分方法 partial void fun();

除表達(dá)式樹外絕大部分特性可兼容.net 2.0下使用。LINQ查詢可通過linqbridge第三方實(shí)現(xiàn)兼容.net 2.0,擴(kuò)展方法可以自行定義ExtensionAttribute來兼容.net 2.0。

C# 4.0

feature keyword/example remark
動(dòng)態(tài)綁定 dynamic o = getObj(); o.fun();
命名和可選參數(shù) void fun(int a1=0){}; fun(a1: 1);
泛型協(xié)變逆變
非默認(rèn)索引器 僅COM支持
內(nèi)嵌COM類型(NoPIA) 運(yùn)行時(shí)脫離COM.interop文件

dynamic特性明確需要CLR4才能支持,之前的版本可以用反射替代。

C# 5.0

feature keyword/example remark
異步方法 async fun(){}; await fun();
調(diào)用者信息 CallerAttribute 用于調(diào)試的特性

await/async為編譯器語法糖,可使用asyncBridge第三方庫(kù)兼容到.net3.5。

C# 6.0

feature keyword/example remark
Roslyn編譯器
導(dǎo)入命名空間 using static System.Math; Cos(1);
異常過濾器 try{} catch(Exception e) when(fun(e)){}
catch/finally中可使用await try{} catch{ await fun();}
自動(dòng)屬性初始化 int Val {get;set;} = 1;
只讀屬性默認(rèn)值 int Val {get;} = 1; 可用于構(gòu)造函數(shù)
表達(dá)式方法體成員 int Val => Math.Sign(1); 用于定義屬性
空傳遞運(yùn)算符 int i = array?.Length; 短路運(yùn)算, 可用于獲取成員和調(diào)用方法
字符串插補(bǔ) var str = $"{a.x}, {a.y}";
nameof運(yùn)算符 var str = nameof(this.Val);
索引器初始化 new Dict{ ["key"]=val };

大部分特性為編譯器語法糖,可兼容到.net 2.0運(yùn)行。其中屬性初始化字符串插補(bǔ)特性編譯至string.Format方法,屬性初始化編譯到構(gòu)造函數(shù),空傳遞運(yùn)算符編譯為if,nameof編譯為常量,索引器初始化識(shí)別this索引。

C# 7.0

feature keyword/example remark
Out聲明變量 int.TryParse(str, out var val);
模式匹配 is/switch/when
元組(Tuple) var a = (x:1, y:2); (int x,int y)=(1,2) 依賴System.ValueTuple
解構(gòu)函數(shù) class P { public void Deconstruction(out int x, out int y){} }; (int a, int b) = new P(); 支持?jǐn)U展函數(shù)
棄元(discards) int.TryParse(str, out _); (_,_) = new P(); 也可用于tuple
局部函數(shù) void fun(){ void fun2(){}; fun2();} 局部函數(shù)可遞歸
數(shù)值字面量分隔符 var x = 0b00_01;var pi=3.14_15_926;var i=1_2_3;
ref變量和返回值 ref int fun(){ return ref ary[0];}; ref int a = ref fun();
表達(dá)式方法體成員 int Val{get=>_val;set=>_val=value;} 可用于定義方法/屬性getter/settter/構(gòu)造函數(shù)/析構(gòu)函數(shù)
廣義異步返回類型 async ValueTask fun(){}; 識(shí)別GetAwaiter方法
Throw表達(dá)式 int Val => throw new Exception();

大部分基于Tuple的特性需要ValueTuple類型支持,需要.net 4.7或以上版本。早期的版本可以通過nuget獲取package來支持。

C# 7.1

feature keyword/example remark
異步主函數(shù) async Task Main(){}
default字面表達(dá)式 int a = default;
元組命名推斷 int x=1,y=2; var a=(x,y);

C# 7.2

feature keyword/example remark
值類型引用語義 in/ref readonly/readonly struct/ref struct
非末尾命名參數(shù) fun(x:1, 2, y:3); 命名參數(shù)和按順序參數(shù)可以混用
數(shù)值字面量分隔符 int a = 0b_0_1; 下劃線可以前導(dǎo)了
private protected class A { private protected int a;} 新增訪問修飾符, 只允許當(dāng)前類及相同assembly內(nèi)子類訪問
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容