1. 程式人生 > >C#9.0 終於來了,您還學的動嗎? 帶上VS一起解讀吧!

C#9.0 終於來了,您還學的動嗎? 帶上VS一起解讀吧!

## 一:背景 ### 1. 講故事 好訊息,`.NET 5.0` 終於在2020年6月10日釋出了第五個預覽版,眼尖的同學一定看到了在這個版本中終於支援了 `C# 9.0`,此處有掌聲,太好了!!! [.Net5官方連結]( https://dotnet.microsoft.com/download/dotnet/5.0 ".Net5官方下載") ![](https://img2020.cnblogs.com/other/214741/202006/214741-20200612070018247-1479429254.png) 可以看到目前的C#9還是預覽版,實現了一部分新語法供開發者提前嚐鮮,從github的roslyn倉庫上可以看到目前準備實現 `17`個新特性,現階段已經實現了`8`個,其中的 `In Progress` 表示正在開發中。 [新特性預覽]( https://github.com/dotnet/roslyn/blob/master/docs/Language%20Feature%20Status.md "新特性預覽") ![](https://img2020.cnblogs.com/other/214741/202006/214741-20200612070018792-950169146.png) ### 2. 安裝必備 * 下載最新的`net5 sdk`吧: dotnet-sdk-5.0.100-preview.5.20279.10-win-x64.exe * 下載最新的 [visual studio 2019 preview 2]( https://docs.microsoft.com/en-us/visualstudio/releases/2019/release-notes-preview "visual studio 2019 preview 2") ![](https://img2020.cnblogs.com/other/214741/202006/214741-20200612070019061-1464422498.png) 找好你自己的vs版本型別哦。。。 ## 二:新特性研究 ## 1. Target-typed new 這個取名一定要留給學易經的大師傅,沒見過世面的我不敢造次,取得不佳影響時運,所謂 `運去金成鐵, 時來鐵似金` ,不過大概意思就是說直接new你定義的區域性變數的型別,用`issues`中總結的話就是: ``` C# Summary: Allow Point p = new (x, y); Shipped in preview in 16.7p1. ``` 接下來就是全部程式碼,看看`使用前` 和 `使用後` 的具體差別。 ``` C# class Program { static void Main(string[] args) { //老語法 var person = new Person("mary", "123456"); //新語法 Person person2 = new("mary", "123456"); Console.WriteLine($"person={person}person2={person2}"); } } public class Person { private string username; private string password; public Person(string username, string password) { this.username = username; this.password = password; } public override string ToString() { return $"username={username},password={password} \n"; } } ``` ![](https://img2020.cnblogs.com/other/214741/202006/214741-20200612070019237-2001282404.png) 然後用ilspy去看看下面的il程式碼,是不是省略了Person,讓自己心裡踏實一點。 ![](https://img2020.cnblogs.com/other/214741/202006/214741-20200612070019408-1080363057.png) > 總的來說這語法還行吧,能起到延長鍵盤使用壽命的功效。 ## 2. Lambda discard parameters 從字面上看大概就是說可以在lambda上使用取消引數,聽起來怪怪的,那本意是什麼呢?有時候lambda上的匿名方法簽名的引數是不需要的,但在以前必須實打實的定義,這樣就會汙染方法體,也就是可以在body中被訪問,如下圖: ![](https://img2020.cnblogs.com/other/214741/202006/214741-20200612070019638-1352171258.png) 但有時候因為客觀原因必須使用`Func`這樣的委託,而且還不想讓方法簽名的引數汙染方法體,我猜測在函數語言程式設計中有這樣的場景吧,可能有點類似MVC中的`EmptyResult`效果。 好了,我想你大概知道啥意思了,接下來實操一把。。。 ``` C# Func func = (_, _) => { return 0; }; var result = func(10, 20); ``` ![](https://img2020.cnblogs.com/other/214741/202006/214741-20200612070019807-1427219605.png) 從圖中可以看到,我在方法體中是找不到所謂的 `_` 變數的,這就神奇了,怎麼做到的呢? 帶著這個好奇心看看它的IL程式碼是個什麼樣子。 ``` C# .method private hidebysig static void Main ( string[] args ) cil managed { // Method begins at RVA 0x2048 // Code size 45 (0x2d) .maxstack 3 .entrypoint .locals init ( [0] class [System.Runtime]System.Func`3 func, [1] int32 result ) IL_0000: nop IL_0001: ldsfld class [System.Runtime]System.Func`3 ConsoleApp1.Program/'<>c'::'<>9__0_0' IL_0006: dup IL_0007: brtrue.s IL_0020 IL_0009: pop IL_000a: ldsfld class ConsoleApp1.Program/'<>c' ConsoleApp1.Program/'<>c'::'<>9' IL_000f: ldftn instance int32 ConsoleApp1.Program/'<>c'::'
b__0_0'(int32, int32) IL_0015: newobj instance void class [System.Runtime]System.Func`3::.ctor(object, native int) IL_001a: dup IL_001b: stsfld class [System.Runtime]System.Func`3 ConsoleApp1.Program/'<>c'::'<>9__0_0' IL_0020: stloc.0 IL_0021: ldloc.0 IL_0022: ldc.i4.s 10 IL_0024: ldc.i4.s 20 IL_0026: callvirt instance !2 class [System.Runtime]System.Func`3::Invoke(!0, !1) IL_002b: stloc.1 IL_002c: ret } // end of method Program::Main ``` 從上面的IL程式碼來看 匿名方法 變成了`<>c`類的`
b__0_0`方法,完整簽名: `ConsoleApp1.Program/'<>c'::'
b__0_0'(int32, int32)`,然後再找一下 `
b__0_0` 方法的定義。 ``` C# .class nested private auto ansi sealed serializable beforefieldinit '<>c' extends [System.Runtime]System.Object .method assembly hidebysig instance int32 '
b__0_0' ( int32 _, int32 _ ) cil managed { // Method begins at RVA 0x2100 // Code size 7 (0x7) .maxstack 1 .locals init ( [0] int32 ) IL_0000: nop IL_0001: ldc.i4.0 IL_0002: stloc.0 IL_0003: br.s IL_0005 IL_0005: ldloc.0 IL_0006: ret } // end of method '<>c'::'
b__0_0' ``` 這說明什麼呢? 說明兩個引數是真實存在的,但編譯器搗了鬼,做了語法上的限制,不讓你訪問所謂的 `_`。 等等。。。有一個問題,IL中的方法簽名怎麼是這樣的: `
b__0_0 (int32 _,int32 _) `, 大家應該知道方法簽名中不可以出現重複的引數名,比如下面這樣定義肯定是報錯的。 ![](https://img2020.cnblogs.com/other/214741/202006/214741-20200612070020013-1557330187.png) 這說明什麼? 說明這個語法糖不僅需要編譯器支援,更需要底層的JIT支援,那怎麼證明呢?我們用windbg去底層挖一挖。。。為了方便除錯,修改如下: ``` C# static void Main(string[] args) { Func func = (_, _) => { Console.WriteLine("進入方法體了!!!"); Console.ReadLine(); return 0; }; var result = func(10, 20); } 0:000> !clrstack -p OS Thread Id: 0x52e8 (0) 0000007035F7E5C0 00007ffaff362655 ConsoleApp1.Program+c.b__0_0(Int32, Int32) [C:\5\ConsoleApp1\ConsoleApp1\Program.cs @ 13] PARAMETERS: this (0x0000007035F7E600) = 0x000001968000cb48 _ (0x0000007035F7E608) = 0x000000000000000a _ (0x0000007035F7E610) = 0x0000000000000014 ``` ![](https://img2020.cnblogs.com/other/214741/202006/214741-20200612070020266-971533924.png) 從圖中可以看到,雖然都是 `_` ,但線上程棧上是完完全全的兩個棧地址。 `0x0000007035F7E608` 和 `0x0000007035F7E610`。 ## 三:總結 總的來說,C#是越來越像函數語言程式設計靠攏,越來越像Scala,就像Jquery的口號一樣: Write less,do more。 好了,先就說這兩個吧,大家先安裝好工具,明天繼續解剖~~~ --- ### 如您有更多問題與我互動,掃描下方進來吧~ ---