第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號(hào)安全,請(qǐng)及時(shí)綁定郵箱和手機(jī)立即綁定

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

標(biāo)簽:
C#

模式匹配 是在 C# 7 中引入的一个非常🐂的特性,你可以在任何类型上使用 模式匹配,甚至是自定义类型,而且在 C# 8 中得到了增强,引入了大量的新模式类型,这篇文章就来讨论如何在 C# 8 中使用模式匹配。

C# 8 中的表达式模式

在 C# 8 中有三种不同的方式来表达这种模式。

  • 位置模式

  • 属性模式

  • Tuple模式

接下来看一下这些模式的相关代码及使用场景。

位置模式

位置模式主要利用类中的 Deconstruct 方法将类中的属性解构到一些零散的变量中,然后实现这些零散变量的比较,如果有点懵的话,考虑下面的 Rectangle 类。


    public class Rectangle
    {
        public int Length { get; set; }
        public int Breadth { get; set; }
        public Rectangle(int x, int y) => (Length, Breadth) = (x, y);
        public void Deconstruct(out int x, out int y) => (x, y) = (Length, Breadth);
    }

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


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

如果还是蒙的话继续看看最终生成的 IL 代码,一目了然。


private static void Main(string[] args)
{
	Rectangle rectangle = new Rectangle(10, 10);
	if (1 == 0)
	{
	}
	if (rectangle == null)
	{
		goto IL_0056;
	}
	rectangle.Deconstruct(out int x, out int y);
	string text;
	if (x != 0)
	{
		if (x != 10)
		{
			goto IL_0056;
		}
		if (y != 5)
		{
			if (y != 10)
			{
				goto IL_0056;
			}
			text = "The value of length and breadth is same – this represents a square.";
		}
		else
		{
			text = "The value of length is 10, breadth is 5.";
		}
	}
	else
	{
		if (y != 0)
		{
			goto IL_0056;
		}
		text = "The value of length and breadth is zero.";
	}
	goto IL_005e;
	IL_0056:
	text = "Default.";
	goto IL_005e;
	IL_005e:
	if (1 == 0)
	{
	}
	string result = text;
	Console.WriteLine(result);
}

C# 8 的 属性模式

属性模式常用于实现基于类中属性的比较,考虑下面的 Employee 类。


    public class Employee
    {
        public int Id { get; set; }
        public string FirstName { get; set; }
        public string LastName { get; set; }
        public decimal Salary { get; set; }
        public string Country { get; set; }
    }

下面的代码片段展示了如何利用 属性模式 实现 employee 的个人所得税计算。


        public static decimal ComputeIncomeTax(Employee employee, decimal salary) => employee switch
        {
            { Country: "Canada" } => (salary * 21) / 100,
            { Country: "UAE" } => 0,
            { Country: "India" } => (salary * 30) / 100,
            _ => 0
        };

接下来看一下如何调用,代码如下。


        static void Main(string[] args)
        {
            Employee employee = new Employee()
            {
                Id = 1,
                FirstName = "Michael",
                LastName = "Stevens",
                Salary = 5000,
                Country = "Canada"
            };
            decimal incometax = ComputeIncomeTax
            (employee, employee.Salary);
            Console.WriteLine("The income tax is {0}", incometax);
            Console.Read();
        }

C# 8 的 tuple模式

Tuple 模式是另一种模式类型,常用于实现同一时刻对多个 input 值进行测试,下面的代码片段展示了如何使用 tuple模式。


        static void Main(string[] args)
        {
            static string GetLanguageNames(string team1, string team2) => (team1, team2) switch
            {
                ("C++", "Java") => "C++ and Java.",
                ("C#", "Java") => "C# and Java.",
                ("C++", "C#") => "C++ and C#.",
                (_, _) => "Invalid input"
            };
            (string, string, string, string) programmingLanguages = ("C++", "Java", "C#", "F#");

            var language1 = programmingLanguages.Item1.ToString();
            
            var language2 = programmingLanguages.Item3.ToString();
            
            Console.WriteLine($"The languages selected are: {GetLanguageNames(language1, language2)}");
        }

C# 8 中对 模式匹配进行了若干种增强,使得代码写起来更加易读,易维护 和 更加高效,也是这么多年程序员翘首以盼的特性之一。

**更多高质量干货:参见我的 GitHub: [csharptranslate] github.com/ctripxchuang/csharptranslate **

點(diǎn)擊查看更多內(nèi)容
TA 點(diǎn)贊

若覺得本文不錯(cuò),就分享一下吧!

評(píng)論

作者其他優(yōu)質(zhì)文章

正在加載中
  • 推薦
  • 評(píng)論
  • 收藏
  • 共同學(xué)習(xí),寫下你的評(píng)論
感謝您的支持,我會(huì)繼續(xù)努力的~
掃碼打賞,你說多少就多少
贊賞金額會(huì)直接到老師賬戶
支付方式
打開微信掃一掃,即可進(jìn)行掃碼打賞哦
今天注冊(cè)有機(jī)會(huì)得

100積分直接送

付費(fèi)專欄免費(fèi)學(xué)

大額優(yōu)惠券免費(fèi)領(lǐng)

立即參與 放棄機(jī)會(huì)
微信客服

購(gòu)課補(bǔ)貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動(dòng)學(xué)習(xí)伙伴

公眾號(hào)

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號(hào)

舉報(bào)

0/150
提交
取消