成人免费xxxxx在线视频软件_久久精品久久久_亚洲国产精品久久久_天天色天天色_亚洲人成一区_欧美一级欧美三级在线观看

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

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

[[376473]]

本文轉載自微信公眾號「碼農讀書」,作者碼農讀書 。轉載本文請聯系碼農讀書公眾號。

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

C# 8 中的表達式模式

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

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

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

位置模式

位置模式主要利用類中的 Deconstruct 方法將類中的屬性解構到一些零散的變量中,然后實現這些零散變量的比較,如果有點懵的話,考慮下面的 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.         } 

如果還是蒙的話繼續看看最終生成的 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 的 屬性模式

屬性模式常用于實現基于類中屬性的比較,考慮下面的 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.     } 

下面的代碼片段展示了如何利用 屬性模式 實現 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.         }; 

接下來看一下如何調用,代碼如下。

  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 模式是另一種模式類型,常用于實現同一時刻對多個 input 值進行測試,下面的代碼片段展示了如何使用 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 中對 模式匹配進行了若干種增強,使得代碼寫起來更加易讀,易維護 和 更加高效,也是這么多年程序員翹首以盼的特性之一。

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

 

責任編輯:武曉燕 來源: 碼農讀書
相關推薦

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# 反射數據

2021-03-07 16:37:52

C#應用程序

2018-08-03 08:37:31

設計模式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

2022-05-17 08:25:10

TypeScript接口前端

2022-06-23 08:00:53

PythonDateTime模塊

2021-06-09 09:36:18

DjangoElasticSearLinux

2021-03-09 07:27:40

Kafka開源分布式

2015-08-27 09:46:09

swiftAFNetworkin

2024-01-18 08:37:33

socketasyncio線程

2011-08-10 09:31:41

Hibernateunion

2019-09-16 19:00:48

Linux變量
點贊
收藏

51CTO技術棧公眾號

主站蜘蛛池模板: 亚洲欧洲激情 | 亚洲永久精品国产 | 久久99精品久久久久久 | 免费视频久久久久 | 国产成人一区二区 | 婷婷综合色 | av永久| 男人影音 | 国产一区91精品张津瑜 | 性高湖久久久久久久久3小时 | 夜夜艹| 国产精品久久久久永久免费观看 | 视频一区在线观看 | 中文字幕一区二区三区四区五区 | 亚洲高清视频一区二区 | 理论片87福利理论电影 | 在线精品观看 | 久久精品亚洲成在人线av网址 | 我要看黄色录像一级片 | 久久最新精品 | 97偷拍视频| 欧美 日韩 国产 成人 在线 91 | 九九看片 | 欧美日韩视频网站 | 日韩视频一区在线观看 | 国产欧美一区二区三区另类精品 | 国产一区二区在线免费观看 | 亚洲综合在线视频 | 成人精品一区二区三区 | 国产一区二区不卡 | 国产亚洲一区二区三区 | 欧美日韩一区二区三区四区 | 国产精品久久毛片av大全日韩 | 国产二区三区 | 精品日本中文字幕 | 一区二区在线不卡 | 在线视频一区二区 | 成人亚洲精品 | 日韩免费高清视频 | 亚州春色| 国产成人jvid在线播放 |