亚洲乱码中文字幕综合,中国熟女仑乱hd,亚洲精品乱拍国产一区二区三区,一本大道卡一卡二卡三乱码全集资源,又粗又黄又硬又爽的免费视频

C# .Net8 switch的用法小結(jié)

 更新時間:2024年05月16日 11:44:31   作者:fkdw  
在 .net 8中,switch 不需要再和傳統(tǒng)的寫法一樣了,會更加的方便,本文主要介紹了C# .Net8 switch的用法小結(jié),具有一定的參考價值,感興趣的可以了解一下

在 .net 8中,switch 不需要再和傳統(tǒng)的寫法一樣了,會更加的方便

創(chuàng)建一個 .net 8 控制臺項目

switch 的寫法沒必要和以前一樣

namespace SwitchTest
{
    internal class Program
    {
        static void Main(string[] args)
        {
            int day = 3;

            var week = day switch
            {
                1 => "Monday",
                2 => "Tuesday",
                3 => "Wednesday",
                4 => "Thursday",
                5 => "Friday",
                _ => "oh shit"
            } ;

            Console.WriteLine(week);
        }
    }
}

運行:

如果將 day 設(shè)置為 30,在所有的選擇中都找不到,那么結(jié)果就自動執(zhí)行 _ 選項代碼

namespace SwitchTest
{
    internal class Program
    {
        static void Main(string[] args)
        {
            int day = 30;

            var week = day switch
            {
                1 => "Monday",
                2 => "Tuesday",
                3 => "Wednesday",
                4 => "Thursday",
                5 => "Friday",
                _ => "oh shit"
            } ;

            Console.WriteLine(week);
        }
    }
}

運行:

遍歷枚舉寫法一樣

namespace SwitchTest
{
    internal class Program
    {
        enum color { red, yellow, green }

        static void Main(string[] args)
        {
            color myColos = color.red;
            string colosStr = myColos switch
            {
                color.red => "紅",
                color.yellow => "黃",
                color.green => "綠",
                _ => throw new Exception()
            } ;
            Console.WriteLine(colosStr);
        }
    }
}

到此這篇關(guān)于C# .Net8 switch的用法小結(jié)的文章就介紹到這了,更多相關(guān).Net8 switch內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

最新評論