当前位置: 首页 > news >正文

c#中switch常用模式

声明模式

首先检查value的类型,然后根据类型输出相应的消息

public void ShowMessage(object value)
{switch (value){case int i: Console.WriteLine($"value is int:{i}"); break;case long l: Console.WriteLine($"value is long:{l}"); break;case bool b: Console.WriteLine($"value is bool:{b}"); break;case string s: Console.WriteLine($"value is string:{s}"); break;default: Console.WriteLine($"value is object"); break;}
}
类型模式

类型模式可以理解为在声明模式中使用了弃元:

public void ShowMessage(object value)
{switch (value){case int: Console.WriteLine($"value is int"); break;case long: Console.WriteLine($"value is long"); break;case bool: Console.WriteLine($"value is bool"); break;case string: Console.WriteLine($"value is string"); break;default: Console.WriteLine($"value is object"); break;}
}
常量模式

常量模式可以理解为原来C#6.0及之前的用法:

switch (score)
{case 10:case 9:Console.WriteLine("优秀");break;case 8:Console.WriteLine("良好");break;case 7:case 6:Console.WriteLine("及格");break;default:Console.WriteLine("不及格");break;
}
关系模式
switch(score)
{case >=80:Console.WriteLine("excellent");break;case >=60:Console.WriteLine("good");break;default:Console.WriteLine("poor");break;
}
逻辑模式
switch(value)
{case 0:Console.WriteLine("value is 0");break;case not 0 and(100 or - 100):Console.WriteLine("abs(value)==100");break;case not 0 and( > 0 and < 100):Console.WriteLine("value is positive and less than 100");break;case not 0 and > 0:Console.WriteLine("value is positive and greater than 100");break;case <-100 or( < 0 and > -100):Console.WriteLine("value is negative and not equals -100");break;
}
属性模式
switch(time)
{case {Year: 2020 or 2021,Month: <= 6,Day: 1}t:Console.WriteLine($ "the first day of every month in the first half of 2020 and 2021");break;case {Year: not 2022}:Console.WriteLine($ "not 2022");break;case {DayOfWeek: not DayOfWeek.Sunday and not DayOfWeek.Saturday}:Console.WriteLine($ "recursion");break;
}
位置模式

位置模式采用解构的特性来说明指定的模式是否匹配:

public record Point2D(int X, int Y); //记录可以解构
static void Print(Point2D point)
{switch(point){case( > 0, > 0):Console.WriteLine("first quadrant");break;case( < 0, > 0):Console.WriteLine("second quadrant");break;case( < 0, < 0):Console.WriteLine("third quadrant");break;case( > 0, < 0):Console.WriteLine("fourth quadrant");break;default:Console.WriteLine("coordinate axis");break;}
}
Var模式

Var模式往往和属性模式和位置模式结合,用于提取属性变量值:

switch(point)
{case(var x,var y,var z):Console.WriteLine($ "3D point:({x},{y},{z})");break; //在位置模式中使用case Point2D{X: var x, Y: var y}:Console.WriteLine($ "2D point:({x},{y})");break; //在属性模式中使用default:Console.WriteLine("others");break;
}
弃元模式

弃元模式在switch语句中用的不多,但是在switch表达式中使用的多:

var result = score
switch
{ >= 80 => "excellent", >= 60 => "good",_ => "poor" //弃元在switch表达式中就相当于default
};
http://www.lryc.cn/news/219724.html

相关文章:

  • Flink SQL 常用作业sql
  • nodejs国内镜像及切换版本工具nvm
  • 用Rust和Scraper库编写图像爬虫的建议
  • Java 语言环境搭建
  • 酷开科技 | 酷开系统里萌萌哒小维在等你!
  • Bash 4关联数组:错误“声明:-A:无效选项”
  • 干货|AI辅助完成论文的正确打开方式!
  • SpringBoot--Web开发篇:含enjoy模板引擎整合,SpringBoot整合springMVC;及上传文件至七牛云;restFul
  • 线上JAVA应用平稳运行一段时间后出现JVM崩溃问题 | 京东云技术团队
  • 进口跨境商城源码:高效、安全、可扩展的电商平台解决方案
  • GEE数据集——2019、2020、2021、2022和2023年全球固定宽带和移动(蜂窝)网络性能Shapefile 格式数据集
  • 什么是防火墙?详解三种常见的防火墙及各自的优缺点
  • 动态规划算法实现0-1背包问题Java语言实现
  • linux查看系统版本
  • pg14-sql基础(四)-多表联查
  • el-date-picker 日期时间选择器 限时时间范围 精确到时分秒
  • 轮廓线dp:GYM103446C
  • 羊驼免疫制备纳米抗体
  • 【AI好好玩02】利用Lama Cleaner本地实现AIGC试玩:擦除对象、替换对象、更换风格等等
  • SQL FULL OUTER JOIN 关键字(完整外部连接)||SQL自连接 Self JOIN
  • 专科医院污水处理设备构造解析及工艺流程
  • 【RabbitMQ】RabbitMQ 消息的可靠性 —— 生产者和消费者消息的确认,消息的持久化以及消费失败的重试机制
  • 百万套行泊一体量产定点,中国市场「开启」智驾高低速集成
  • Gopro hero5运动相机格式化后恢复案例
  • Microsoft Dynamics 365 CE 扩展定制 - 6. 增强代码
  • 基于libopenh264 codec的svc分层流实现方案
  • 为机器学习算法准备数据(Machine Learning 研习之八)
  • 基于Python OpenCV的金铲铲自动进游戏、D牌...
  • c++中httplib使用
  • Vite 的基本原理,和 webpack 在开发阶段的比较