自拍偷在线精品自拍偷,亚洲欧美中文日韩v在线观看不卡

如何在 C# 8 中使用 模式匹配

開發(fā) 后端
模式匹配 是在 C# 7 中引入的一個非常??的特性,你可以在任何類型上使用 模式匹配,甚至是自定義類型,而且在 C# 8 中得到了增強,引入了大量的新模式類型,這篇文章就來討論如何在 C# 8 中使用模式匹配。

[[376473]]

本文轉(zhuǎn)載自微信公眾號「碼農(nóng)讀書」,作者碼農(nóng)讀書 。轉(zhuǎn)載本文請聯(lián)系碼農(nóng)讀書公眾號。

模式匹配 是在 C# 7 中引入的一個非常??的特性,你可以在任何類型上使用 模式匹配,甚至是自定義類型,而且在 C# 8 中得到了增強,引入了大量的新模式類型,這篇文章就來討論如何在 C# 8 中使用模式匹配。

C# 8 中的表達(dá)式模式

在 C# 8 中有三種不同的方式來表達(dá)這種模式。

  • 位置模式
  • 屬性模式
  • Tuple模式

接下來看一下這些模式的相關(guān)代碼及使用場景。

位置模式

位置模式主要利用類中的 Deconstruct 方法將類中的屬性解構(gòu)到一些零散的變量中,然后實現(xiàn)這些零散變量的比較,如果有點懵的話,考慮下面的 Rectangle 類。

  1. public class Rectangle 
  2.    { 
  3.        public int Length { get; set; } 
  4.        public int Breadth { get; set; } 
  5.        public Rectangle(int x, int y) => (Length, Breadth) = (x, y); 
  6.        public void Deconstruct(out int x, out int y) => (x, y) = (Length, Breadth); 
  7.    } 

接下來看一下如何在 Rectangle 上使用 位置模式。

  1. static void Main(string[] args) 
  2.         { 
  3.             Rectangle rectangle = new Rectangle(10, 10); 
  4.             var result = rectangle switch 
  5.             { 
  6.                 Rectangle(0, 0) => "The value of length and breadth is zero."
  7.                 Rectangle(10, 10) => "The value of length and breadth is same – this represents a square."
  8.                 Rectangle(10, 5) => "The value of length is 10, breadth is 5."
  9.                 _ => "Default." 
  10.             }; 
  11.             Console.WriteLine(result); 
  12.         } 

如果還是蒙的話繼續(xù)看看最終生成的 IL 代碼,一目了然。

  1. private static void Main(string[] args) 
  2.  Rectangle rectangle = new Rectangle(10, 10); 
  3.  if (1 == 0) 
  4.  { 
  5.  } 
  6.  if (rectangle == null
  7.  { 
  8.   goto IL_0056; 
  9.  } 
  10.  rectangle.Deconstruct(out int x, out int y); 
  11.  string text; 
  12.  if (x != 0) 
  13.  { 
  14.   if (x != 10) 
  15.   { 
  16.    goto IL_0056; 
  17.   } 
  18.   if (y != 5) 
  19.   { 
  20.    if (y != 10) 
  21.    { 
  22.     goto IL_0056; 
  23.    } 
  24.    text = "The value of length and breadth is same – this represents a square."
  25.   } 
  26.   else 
  27.   { 
  28.    text = "The value of length is 10, breadth is 5."
  29.   } 
  30.  } 
  31.  else 
  32.  { 
  33.   if (y != 0) 
  34.   { 
  35.    goto IL_0056; 
  36.   } 
  37.   text = "The value of length and breadth is zero."
  38.  } 
  39.  goto IL_005e; 
  40.  IL_0056: 
  41.  text = "Default."
  42.  goto IL_005e; 
  43.  IL_005e: 
  44.  if (1 == 0) 
  45.  { 
  46.  } 
  47.  string result = text; 
  48.  Console.WriteLine(result); 

C# 8 的 屬性模式

屬性模式常用于實現(xiàn)基于類中屬性的比較,考慮下面的 Employee 類。

  1. public class Employee 
  2.     { 
  3.         public int Id { get; set; } 
  4.         public string FirstName { get; set; } 
  5.         public string LastName { get; set; } 
  6.         public decimal Salary { get; set; } 
  7.         public string Country { get; set; } 
  8.     } 

下面的代碼片段展示了如何利用 屬性模式 實現(xiàn) employee 的個人所得稅計算。

  1. public static decimal ComputeIncomeTax(Employee employee, decimal salary) => employee switch 
  2.         { 
  3.             { Country: "Canada" } => (salary * 21) / 100, 
  4.             { Country: "UAE" } => 0, 
  5.             { Country: "India" } => (salary * 30) / 100, 
  6.             _ => 0 
  7.         }; 

接下來看一下如何調(diào)用,代碼如下。

  1. static void Main(string[] args) 
  2.         { 
  3.             Employee employee = new Employee() 
  4.             { 
  5.                 Id = 1, 
  6.                 FirstName = "Michael"
  7.                 LastName = "Stevens"
  8.                 Salary = 5000, 
  9.                 Country = "Canada" 
  10.             }; 
  11.             decimal incometax = ComputeIncomeTax 
  12.             (employee, employee.Salary); 
  13.             Console.WriteLine("The income tax is {0}", incometax); 
  14.             Console.Read(); 
  15.         } 

C# 8 的 tuple模式

Tuple 模式是另一種模式類型,常用于實現(xiàn)同一時刻對多個 input 值進(jìn)行測試,下面的代碼片段展示了如何使用 tuple模式。

  1. static void Main(string[] args) 
  2.         { 
  3.             static string GetLanguageNames(string team1, string team2) => (team1, team2) switch 
  4.             { 
  5.                 ("C++""Java") => "C++ and Java."
  6.                 ("C#""Java") => "C# and Java."
  7.                 ("C++""C#") => "C++ and C#."
  8.                 (_, _) => "Invalid input" 
  9.             }; 
  10.             (string, string, string, string) programmingLanguages = ("C++""Java""C#""F#"); 
  11.  
  12.             var language1 = programmingLanguages.Item1.ToString(); 
  13.              
  14.             var language2 = programmingLanguages.Item3.ToString(); 
  15.              
  16.             Console.WriteLine($"The languages selected are: {GetLanguageNames(language1, language2)}"); 
  17.         } 

C# 8 中對 模式匹配進(jìn)行了若干種增強,使得代碼寫起來更加易讀,易維護(hù) 和 更加高效,也是這么多年程序員翹首以盼的特性之一。

譯文鏈接:https://www.infoworld.com/article/3518431/how-to-use-pattern-matching-in-csharp-80.html

 

責(zé)任編輯:武曉燕 來源: 碼農(nóng)讀書
相關(guān)推薦

2021-02-01 12:36:59

C# Channels存儲

2021-01-19 05:30:55

C# 8異步流IEnumerable

2021-01-22 05:53:08

C# IndexRange

2021-01-28 05:14:40

C#接口簽名

2020-12-31 07:31:10

C# 反射數(shù)據(jù)

2021-03-07 16:37:52

C#應(yīng)用程序

2018-08-03 08:37:31

設(shè)計模式IT項目GDPR

2009-08-04 10:29:06

在C#中使用存儲過程

2021-11-25 00:04:16

C# 插值字符串

2024-12-03 08:00:00

2020-01-07 09:50:41

Windows 10上帝模式Windows

2009-08-31 16:12:02

C#使用Singlet

2011-08-10 09:31:41

Hibernateunion

2021-03-09 07:27:40

Kafka開源分布式

2015-08-27 09:46:09

swiftAFNetworkin

2021-06-09 09:36:18

DjangoElasticSearLinux

2022-05-17 08:25:10

TypeScript接口前端

2022-06-23 08:00:53

PythonDateTime模塊

2024-01-18 08:37:33

socketasyncio線程

2022-10-13 14:28:40

Brave瀏覽器畫中畫
點贊
收藏

51CTO技術(shù)棧公眾號