日本综合一区二区|亚洲中文天堂综合|日韩欧美自拍一区|男女精品天堂一区|欧美自拍第6页亚洲成人精品一区|亚洲黄色天堂一区二区成人|超碰91偷拍第一页|日韩av夜夜嗨中文字幕|久久蜜综合视频官网|精美人妻一区二区三区

RELATEED CONSULTING
相關(guān)咨詢
選擇下列產(chǎn)品馬上在線溝通
服務(wù)時(shí)間:8:30-17:00
你可能遇到了下面的問題
關(guān)閉右側(cè)工具欄

新聞中心

這里有您想知道的互聯(lián)網(wǎng)營(yíng)銷解決方案
如何在C#8中使用模式匹配

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

成都創(chuàng)新互聯(lián)成立10余年來,這條路我們正越走越好,積累了技術(shù)與客戶資源,形成了良好的口碑。為客戶提供網(wǎng)站制作、成都網(wǎng)站設(shè)計(jì)、網(wǎng)站策劃、網(wǎng)頁(yè)設(shè)計(jì)、國(guó)際域名空間、網(wǎng)絡(luò)營(yíng)銷、VI設(shè)計(jì)、網(wǎng)站改版、漏洞修補(bǔ)等服務(wù)。網(wǎng)站是否美觀、功能強(qiáng)大、用戶體驗(yàn)好、性價(jià)比高、打開快等等,這些對(duì)于網(wǎng)站建設(shè)都非常重要,成都創(chuàng)新互聯(lián)通過對(duì)建站技術(shù)性的掌握、對(duì)創(chuàng)意設(shè)計(jì)的研究為客戶提供一站式互聯(lián)網(wǎng)解決方案,攜手廣大客戶,共同發(fā)展進(jìn)步。

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

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

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

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

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

位置模式

位置模式主要利用類中的 Deconstruct 方法將類中的屬性解構(gòu)到一些零散的變量中,然后實(shí)現(xiàn)這些零散變量的比較,如果有點(diǎ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. {
  3.  Rectangle rectangle = new Rectangle(10, 10);
  4.  if (1 == 0)
  5.  {
  6.  }
  7.  if (rectangle == null)
  8.  {
  9.   goto IL_0056;
  10.  }
  11.  rectangle.Deconstruct(out int x, out int y);
  12.  string text;
  13.  if (x != 0)
  14.  {
  15.   if (x != 10)
  16.   {
  17.    goto IL_0056;
  18.   }
  19.   if (y != 5)
  20.   {
  21.    if (y != 10)
  22.    {
  23.     goto IL_0056;
  24.    }
  25.    text = "The value of length and breadth is same – this represents a square.";
  26.   }
  27.   else
  28.   {
  29.    text = "The value of length is 10, breadth is 5.";
  30.   }
  31.  }
  32.  else
  33.  {
  34.   if (y != 0)
  35.   {
  36.    goto IL_0056;
  37.   }
  38.   text = "The value of length and breadth is zero.";
  39.  }
  40.  goto IL_005e;
  41.  IL_0056:
  42.  text = "Default.";
  43.  goto IL_005e;
  44.  IL_005e:
  45.  if (1 == 0)
  46.  {
  47.  }
  48.  string result = text;
  49.  Console.WriteLine(result);
  50. }

C# 8 的 屬性模式

屬性模式常用于實(shí)現(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.     }

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

 
 
 
 
  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 模式是另一種模式類型,常用于實(shí)現(xiàn)同一時(shí)刻對(duì)多個(gè) input 值進(jìn)行測(cè)試,下面的代碼片段展示了如何使用 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.             var language1 = programmingLanguages.Item1.ToString();
  12.             
  13.             var language2 = programmingLanguages.Item3.ToString();
  14.             
  15.             Console.WriteLine($"The languages selected are: {GetLanguageNames(language1, language2)}");
  16.         }

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

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


當(dāng)前文章:如何在C#8中使用模式匹配
URL分享:http://www.dlmjj.cn/article/cccscos.html