首页 > 编程语言 >【C#9.0篇】Switch匹配模式新特性

【C#9.0篇】Switch匹配模式新特性

时间:2023-10-22 14:12:33浏览次数:33  
标签:Console C# 9.0 Price CSharp9 Switch ChapterSwitch new electronic

概述

C#9.0中的switch 匹配模式引入了许多新特性,其中一些特性是C#8.0中不存在的,比如以下特性:

  • 关系模式:可以使用关系运算符
  • 模式匹配+逻辑模式:可以使用模式匹配+逻辑模式(如and、or)来组合多个模式
  • 模式匹配+关系模式:可以使用模式匹配+关系运算符(如<、>、<=、>=、==、!=)
  • 默认的匹配不必使用丢弃“_”来实现
    下面我们举例说明一下这些特性:

示例

下面我将使用dotnet cli 创建一个dotnet 6.0的项目。

环境信息

Platform:Windows 11

Dotnet SDK:6.0

IDE:VS Code

首先我们按照以下步骤创建项目:

步骤一:创建项目文件夹

mkdir CSharp9.ChapterSwitch.Sample
cd CSharp9.ChapterSwitch.Sample

步骤二:创建项目文件

dotnet new sln -n CSharp9.ChapterSwitch.Sample
mkdir src
cd src

步骤三:创建类库以及控制台

dotnet new console -n CSharp9.ChapterSwitch.Console --framework net6.0
dotnet new classlib -n CSharp9.ChapterSwitch.Models --framework net6.0

步骤四:将类库以及控制台添加到项目上

cd ..
dotnet sln add src/CSharp9.ChapterSwitch.Console/CSharp9.ChapterSwitch.Console.csproj
dotnet sln add src/CSharp9.ChapterSwitch.Models/CSharp9.ChapterSwitch.Models.csproj
dotnet add src/CSharp9.ChapterSwitch.Console/CSharp9.ChapterSwitch.Console.csproj reference src/CSharp9.ChapterSwitch.Models/CSharp9.ChapterSwitch.Models.csproj

步骤五:示例代码

1、Models 类库代码

在Models类库中,按照以下代码片段添加相关的类文件,请注意类的命名空间

fragment 1

namespace CSharp9.ChapterSwitch.Models;
public class Electronic
{
    public double Price { get; set; }
    public string Name { get; set; }
}
namespace CSharp9.ChapterSwitch.Models;
public class MobilePhone : Electronic
{
}
namespace CSharp9.ChapterSwitch.Models;
public class Camera : Electronic
{
    public double Pixel { get; set; }
}
namespace CSharp9.ChapterSwitch.Models;
public class Computer : Electronic
{
    public int LightingInterface { get; set; }
}

2、Console 代码

在Console控制台的Program中添加 code fragment2的代码片段

fragment 2

using CSharp9.ChapterSwitch.Models;
using System;

class Program
{
    static void Main(string[] args)
    {
        SwitchExpression();
    }

    static void SwitchExpression()
    {
        Electronic[] electronics =
        {
            new MobilePhone{Name="XIAOMI5",Price=5000},
            new MobilePhone{Name="IPhone13Pro",Price=9000},
            new MobilePhone{Name="HUAWEIMate50Pro",Price=7000},
            new MobilePhone{Name="HUAWEIP50Pro",Price=8000},
            new Camera{Name="Canon80D",Price=7000,Pixel=12000_000},
            new Computer{Name="DELLXPS7590",Price=8600,LightingInterface=1},
            new Computer{Name="DELLXPS9360",Price=12000,LightingInterface=3}
        };

        foreach (var electronic in electronics)
        {
             {
                //c#8.0
                var desc = electronic switch
                {
                    MobilePhone s => s.Price switch
                    {
                        double a when a <= 5000 => "Cost performance electronic",
                        double a when a <= 7000 => "Slightly expensive electronic",
                        double a when a <= 9000 => "Light luxury electronic",
                        _ => "Other"
                    },
                    Camera s when s.Price <= 7000 && s.Pixel <= 12000_000 => "Cost performance electronic",
                    Camera _ => "Other",
                    Computer s when s.Price <= 7000 && s.LightingInterface >= 1 => "Cost performance electronic",
                    Computer s when s.Price <= 10000 && s.LightingInterface >= 2 => "Slightly expensive electronic",
                    Computer s when s.Price <= 12000 && s.LightingInterface >= 3 => "Light luxury electronic",
                    Computer _ => "Other",
                    _ => throw new NotImplementedException()
                };
                Console.WriteLine($"Current electronic is {electronic.Name},price:{electronic.Price:C},is {desc}.");
            }
        }
        
        Console.Read();
    }
}

3、Console 项目设置

fragment 3

<PropertyGroup>
    <OutputType>Exe</OutputType>
    <TargetFramework>net6.0</TargetFramework>
    <ImplicitUsings>disable</ImplicitUsings>
    <Nullable>disable</Nullable>
    <LangVersion>8</LangVersion>
</PropertyGroup>

运行代码:

dotnet run --project src/CSharp9.ChapterSwitch.Console/CSharp9.ChapterSwitch.Console.csproj

如上例,有一个 Electronic父类,Mobile、Camera、Computer分别继承Electronic类,并且每个自类有可能有自己单独的属性,然后在Program中声明一个Electronic数组,并且循环输出信息,循环判断并输出当前电子产品的定位,性价比、稍贵、轻奢品

以上Console中的Program是使用的C#8.0的语法编写Switch的匹配模式,下面我们使用C#9.0的Switch匹配模式新特性来重构这段代码:

4、匹配模式新特性

设置Console.csproj的 LangVersion=9,重构以上Console中代码,重新运行项目

fragment 4

using CSharp9.ChapterSwitch.Models;
using System;

class Program
{
    static void Main(string[] args)
    {
        SwitchExpression();
    }

    static void SwitchExpression()
    {
        Electronic[] electronics =
        {
            new MobilePhone{Name="XIAOMI5",Price=5000},
            new MobilePhone{Name="IPhone13Pro",Price=9000},
            new MobilePhone{Name="HUAWEIMate50Pro",Price=7000},
            new MobilePhone{Name="HUAWEIP50Pro",Price=8000},
            new Camera{Name="Canon80D",Price=7000,Pixel=12000_000},
            new Computer{Name="DELLXPS7590",Price=8600,LightingInterface=1},
            new Computer{Name="DELLXPS9360",Price=12000,LightingInterface=3}
        };

        Console.WriteLine($"C# 8.0");
        foreach (var electronic in electronics)
        {
            {
                //c#8.0
                var desc = electronic switch
                {
                    MobilePhone s => s.Price switch
                    {
                        double a when a <= 5000 => "Cost performance electronic",
                        double a when a <= 7000 => "Slightly expensive electronic",
                        double a when a <= 9000 => "Light luxury electronic",
                        _ => "Other"
                    },
                    Camera s when s.Price <= 7000 && s.Pixel <= 12000_000 => "Cost performance electronic",
                    Camera _ => "Other",
                    Computer s when s.Price <= 7000 && s.LightingInterface >= 1 => "Cost performance electronic",
                    Computer s when s.Price <= 10000 && s.LightingInterface >= 2 => "Slightly expensive electronic",
                    Computer s when s.Price <= 12000 && s.LightingInterface >= 3 => "Light luxury electronic",
                    Computer _ => "Other",
                    _ => throw new NotImplementedException()
                };
                Console.WriteLine($"Current electronic is {electronic.Name},price:{electronic.Price:C},is {desc}.");
            }

        };

        Console.WriteLine($"C# 9.0 写法1");
        foreach (var electronic in electronics)
        {
            {
                // c#9.0 写法1
                var desc = electronic switch
                {
                    MobilePhone s => s.Price switch //关系模式
                    {
                        <= 5000 => "Cost performance electronic",
                        <= 7000 => "Slightly expensive electronic",
                        <= 9000 => "Light luxury electronic",
                        _ => "Other"
                    },
                    Camera { Price: <= 7000, Pixel: <= 12000_000 } => "Cost performance electronic", // 模式变量+关系模式
                    Camera => "Other",// 无需丢弃"_"
                    Computer { Price: <= 7000, LightingInterface: >= 1 } => "Cost performance electronic",
                    Computer { Price: <= 10000, LightingInterface: >= 2 } => "Slightly expensive electronic",
                    Computer { Price: <= 12000, LightingInterface: >= 3 } => "Light luxury electronic",
                    Computer => "Other",
                    _ => throw new NotImplementedException()
                };
                Console.WriteLine($"Current electronic is {electronic.Name},price:{electronic.Price:C},is {desc}.");
            }

        };

        Console.WriteLine($"C# 9.0 写法2");
        foreach (var electronic in electronics)
        {
            {
                // c#9.0 写法2
                var desc = electronic switch
                {
                    MobilePhone { Price: <= 5000 } or Camera { Price: <= 7000, Pixel: <= 12000_000 } or Computer { Price: <= 7000, LightingInterface: >= 1 } => "Cost performance electronic",//模式变量+关系模式+逻辑或
                    MobilePhone s => s.Price switch 
                    {
                        <= 5000 => "Cost performance electronic",
                        <= 7000 => "Slightly expensive electronic",
                        <= 9000 => "Light luxury electronic",
                        _ => "Other"
                    },
                    Camera => "Other",
                    Computer { Price: <= 10000 } and Computer { LightingInterface: >= 2 } => "Slightly expensive electronic", //模式变量+关系模式+逻辑与
                    Computer { Price: <= 12000 } and Computer { LightingInterface: >= 3 } => "Light luxury electronic",
                    Computer => "Other",
                    _ => throw new NotImplementedException()
                };
                Console.WriteLine($"Current electronic is {electronic.Name},price:{electronic.Price:C},is {desc}.");
            }
        }
        Console.Read();
    }
}

重新运行代码

dotnet run --project src/CSharp9.ChapterSwitch.Console/CSharp9.ChapterSwitch.Console.csproj

标签:Console,C#,9.0,Price,CSharp9,Switch,ChapterSwitch,new,electronic
From: https://www.cnblogs.com/harley-chang/p/17780381.html

相关文章

  • CmakeList.txt语法
    CmakeList语法PROJECT(hello)#PROJECT(名称,语言):指定工程名称和支持的语言#PROJECT(hello,CCXX):指定了工程名称,支持C和C++SET(SRC_LISTmain.cpp)#SET():用来显示指定变量#SET(SRC_LISTmain.cpptest.cpp)MESSAGE(STATUS"ThisisBINARYdir"${HELLO_BINARY_......
  • 如何解决“未在本地计算机上注册“Microsoft.ACE.OLEDB.12.0”提供程序”问题
    最近在做Excel文件导入时候,出现"未在本地计算机上注册“Microsoft.ACE.OLEDB.12.0”提供程序" 问题 产生原因:这个问题一般是在导入Excel文件的时候报的错,原因是缺少了相对应的MicrosoftAccessDatabaseEngine组件。解决方法:安装AccessDatabaseEngine插件1)访问下载路径(http......
  • Python scrapy爬虫框架爬取廖雪峰大佬官网Python教程左侧目录
    文章转载至:mob6454cc6f27a3 的PythonScrapy爬虫框架实例(一)爬虫框架scrapy简单实例吃面崩掉牙的Scrapy爬虫框架入门教程(1)——爬取廖雪峰老师的博客!!只是爬取一个字段,并不将爬取的数据存储下来。!!运行环境:window10x64系统python3.6pycharmanacondascrapy安装好以上环境、包之后,......
  • Awk、Sed、Grep、Cut命令基本用法
    awk、sed、grep、cut是Linux中文本处理、shell开发、数据截取的利器。Sedsed(StreamEditor)是一种流式文本编辑器,常用于对文本文件进行处理和转换。下面是一些常见的sed经典用法:替换文本:sed's/old/new/g'file.txt将文件中所有的"old"替换为"new"。2.删除行:sed'/pattern/d'fil......
  • CyclicBarrier(同步屏障)了解吗?
    CyclicBarrier的字面意思是可循环使用(Cyclic)的屏障(Barrier)。它要做的事情是,让一组线程到达一个屏障(也可以叫同步点)时被阻塞,直到最后一个线程到达屏障时,屏障才会开门,所有被屏障拦截的线程才会继续运行。它和CountDownLatch类似,都可以协调多线程的结束动作,在它们结束后都可以执行特......
  • VMware Workstation 17.5 Pro Unlocker & OEM BIOS for Windows & Linux
    VMwareWorkstation17.5ProUnlocker&OEMBIOSforWindows&LinuxmacOSUnlocker,支持macOSSonoma请访问原文链接:https://sysin.org/blog/vmware-workstation-17-unlocker/,查看最新版。原创作品,转载请保留出处。作者主页:sysin.org桌面HypervisorVMwareWorkstation......
  • VMware Workstation 17.5 Pro Unlocker & OEM BIOS for Windows
    VMwareWorkstation17.5ProUnlocker&OEMBIOSforWindowsmacOSUnlocker,支持macOSSonoma请访问原文链接:https://sysin.org/blog/vmware-workstation-17-unlocker/,查看最新版。原创作品,转载请保留出处。作者主页:sysin.org桌面HypervisorVMwareWorkstationProVMwa......
  • VMware Workstation 17.5 Pro Unlocker & OEM BIOS for Linux
    VMwareWorkstation17.5ProUnlocker&OEMBIOSforLinuxmacOSUnlocker,支持macOSSonoma请访问原文链接:https://sysin.org/blog/vmware-workstation-17-unlocker/,查看最新版。原创作品,转载请保留出处。作者主页:sysin.org桌面HypervisorVMwareWorkstationProVMware......
  • VMware Fusion 13.5 for Mac
    VMwareFusion13.5forMacVMwareFusion13forIntel-basedandApplesiliconMacs请访问原文链接:https://sysin.org/blog/vmware-fusion-13/,查看最新版。原创作品,转载请保留出处。作者主页:sysin.org使用VMwareFusion在虚拟机(VM)中运行Windows、Linux、容器、Kube......
  • DHCP
        ......