C#7.0新語法


一、out輸出參數

在以前使用out輸出參數的時候,必須先定義變量,然后才能使用,例如:

先定義一個方法,方法參數是out類型的輸出參數:

1 private void DoNoting(out int x, out int y)
2 {
3      x = 1;
4      y = 2;
5 }

以前版本的寫法:

1 // 必須先定義i、j,才能使用out參數
2 int i = 0;
3 int j = 0;
4 this.DoNoting(out i, out j);
5 Console.WriteLine($"i+j={i+j}");

 在C#7.0中,可以不用先定義,就能夠直接使用了:

1 this.DoNoting(out int x, out int y);
2 Console.WriteLine($"x+y={x + y}");
3 this.DoNoting(out var l, out var m);

 結果:

 

二、模式

 1 /// <summary>
 2 /// 具有模式的 IS 表達式
 3 /// </summary>
 4 /// <param name="o"></param>
 5 public void PrintStars(object o)
 6 {
 7       if (o is null) return;     // 常量模式 "null"
 8       if (!(o is int i)) return; // 類型模式 定義了一個變量 "int i" i的值就是o的值 相當於is類型判斷
 9       Console.WriteLine($"i={i}");
10 }

使用方法:

1 this.PrintStars(null);
2 this.PrintStars(3);

 結果:

除了可以像上面那樣使用外,還可以使用下面的方式:

 1 private void Switch(string text)
 2 {
 3             int k = 100;
 4             switch (text)
 5             {
 6                 case "Tom" when k > 10:   // text="Tom"且k<10才會輸出Tom
 7                     Console.WriteLine("Tom");
 8                     break;
 9                 case "Joe" when text.Length < 10:  //text="Joe"且text的長度<10才會輸出Joe
10                     Console.WriteLine("Joe");
11                     break;
12                 case string s when s.Length > 7://模式 定義變量s,s就是text的值
13                     Console.WriteLine(s);
14                     break;
15                 default:
16                     Console.WriteLine("default");
17                     break;
18                 case null:
19                     Console.WriteLine("null");
20                     break;
21             }
22 }

 調用:

1 this.Switch(null);
2 this.Switch("TomTomKevin");
3 this.Switch("JoeForest");

三、元組

先來看下面的兩個方法:

 1 /// <summary>
 2 /// 使用默認參數名稱
 3 /// </summary>
 4 /// <param name="id"></param>
 5 /// <returns></returns>
 6 private (string, string, string) LookupName(long id) // tuple return type
 7 {
 8       return ("first", "middle", "last");
 9 }
10 
11 /// <summary>
12 /// 不使用默認參數名稱
13 /// </summary>
14 /// <param name="id"></param>
15 /// <returns></returns>
16 private (string first, string middle, string last) LookupNameByName(long id) // tuple return type
17 {
18      return ("first", "middle", "last");
19      //return (first: "first", middle: "middle", last: "last");
20 }

 調用:

 1 // 使用默認參數名稱:Item1、Item2、Item3
 2 Console.WriteLine($"使用默認參數名稱");
 3 var result = this.LookupName(1);
 4 Console.WriteLine(result.Item1);
 5 Console.WriteLine(result.Item2);
 6 Console.WriteLine(result.Item3);
 7 // 不使用默認參數名稱
 8 Console.WriteLine($"不使用默認參數名稱");
 9 var result2 = this.LookupNameByName(1);
10 Console.WriteLine(result2.first);
11 Console.WriteLine(result2.middle);
12 Console.WriteLine(result2.last);
13 // 也可以使用默認參數名稱
14 Console.WriteLine($"也可以使用默認參數名稱");
15 Console.WriteLine(result2.Item1);
16 Console.WriteLine(result2.Item2);
17 Console.WriteLine(result2.Item3);

 結果:

四、數字分割

如果數字太長,可以按照一定的位數用“_”進行分割,例如:

1 long big = 100_000_0000;
2 Console.WriteLine($"big={big}");

 


免責聲明!

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



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