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

公司网站做推广网络工程师培训课程

公司网站做推广,网络工程师培训课程,安丘网站建设aqfeifan,深圳制作网站模式匹配#xff08;Pattern Matching#xff09;是 C# 中一个强大且灵活的特性#xff0c;允许开发者以更直观的方式检查数据结构#xff0c;并根据特定模式执行操作。 随着 C# 语言版本的发展#xff0c;模式匹配的功能越来越丰富#xff0c;为处理复杂数据提供了极大…模式匹配Pattern Matching是 C# 中一个强大且灵活的特性允许开发者以更直观的方式检查数据结构并根据特定模式执行操作。 随着 C# 语言版本的发展模式匹配的功能越来越丰富为处理复杂数据提供了极大的便利。 本文将深入介绍 C# 中的模式匹配并提供各种使用场景的示例。 目录 1. 类型模式 2.常量模式 3.属性模式 4.位置模式 5.组合模式 6. when 子句 7. switch 表达式 8.递归模式 1. 类型模式 说明 类型模式用于检查一个对象是否是某个特定的类型。如果匹配成功可以直接将对象解构为该类型的变量。 示例 object data 42;if (data is int number) {Console.WriteLine($The number is {number}.); }object text Hello, C#;if (text is string message) {Console.WriteLine($Message length: {message.Length}); }2.常量模式 说明 常量模式用于检查对象的值是否与某个常量相等通常在 switch 语句中使用。 示例 string command start;switch (command) {case start:Console.WriteLine(Starting...);break;case stop:Console.WriteLine(Stopping...);break;default:Console.WriteLine(Unknown command);break; }3.属性模式 说明 属性模式用来检查对象的属性是否满足某些条件。它使用冒号 : 来分隔属性名称和匹配条件。 示例 public class Rectangle {public int Width { get; set; }public int Height { get; set; } }Rectangle rect new Rectangle { Width 10, Height 5 };if (rect is { Width: 5, Height: 10 }) {Console.WriteLine(Rectangle is within the desired dimensions.); }4.位置模式 说明 位置模式适用于元组和记录类型允许通过解构检查对象的元素。 示例 var point (X: 10, Y: 20);if (point is (10, 20)) {Console.WriteLine(Point is at the expected location.); }public record Circle(int Radius, (int X, int Y) Center);Circle circle new Circle(5, (0, 0));if (circle is Circle(5, (0, 0))) {Console.WriteLine(Circle is at the origin with radius 5.); }5.组合模式 说明 组合模式允许使用逻辑运算符将多个模式组合在一起形成更复杂的匹配条件。 示例 object item 25;if (item is int i i 10) {Console.WriteLine(The integer is greater than 10.); }string input example;if (input is not null input.Length 5) {Console.WriteLine(The input string is longer than 5 characters.); }6. when 子句 说明 when 子句允许在模式匹配中添加额外的条件判断。它可以与 switch 语句结合使用。 示例 int score 85;switch (score) {case int n when n 90:Console.WriteLine(Grade: A);break;case int n when n 80:Console.WriteLine(Grade: B);break;default:Console.WriteLine(Below B);break; }7. switch 表达式 说明 switch 表达式引入了一种简洁的语法来表达基于模式的分支。 示例 string status running;string message status switch {running The system is running.,stopped The system has stopped.,_ Unknown status. };Console.WriteLine(message);8.递归模式 说明 递归模式允许对嵌套的数据结构进行深层次的模式匹配。 示例 public record TreeNode(string Name, TreeNode? Left, TreeNode? Right);var root new TreeNode(Root, new TreeNode(Left, null, null), new TreeNode(Right, null, null));if (root is TreeNode(Root, TreeNode(Left, null, null), TreeNode(Right, null, null))) {Console.WriteLine(Matched the entire tree structure.); }不同类型的模式匹配适用于不同的场景可以显著提高代码的可读性和维护性。希望本文能帮助你更好地理解和应用 C# 中的模式匹配特性。如果你有任何疑问或需要进一步的帮助请随时留言交流
http://www.sczhlp.com/news/170189/

相关文章:

  • 新手做导航网站比 wordpress
  • 建筑工程网站开发怎么做一个国外网站
  • 济宁市城市建设局网站厦门做网站seo
  • 快速做效果图的网站叫什么西安seo外包公司
  • 可以找厂家的网站怎么做网页插件
  • 小程序网站开发公司WordPress目录怎么发表文章
  • Mac怎么搭建网站开发环境海南网站建设推广
  • 网站秒收录怎么做的网站的首页文案
  • 网站手机版建设网站站内优化方法
  • 做爰全过程免费视频网站合肥建设工程质量监督局网站
  • 如何让网站做网页适配网页设计基础试题
  • 网站制作公司的swot分析WordPress生成海报插件
  • .mom域名可以做网站吗怎样将字体安装在wordpress
  • 国内专业seo公司北京网站优化快速排名
  • wordpress成品网站免费好123设为主页
  • 做国外网站用国内服务器高端网站建设创新
  • 招聘网站建设维护人员深圳网站建设网络公司
  • 网站空间在哪买好专门做推荐的网站
  • 学生管理系统面向对象分析报告
  • 荷兰青少年通过Telegram被招募,涉嫌参与俄罗斯支持的黑客活动
  • 企业网站策划方案书建站视频教程网
  • 公司名称 网站域名 关联wordpress 绕过后台
  • 网站设计英文报告网站建设徐州百度网络网站
  • 励志做的很好的网站seo排名教程
  • 鼎豪网站建设windows server2012 wordpress
  • dw网页制作教程个人网站租赁合同模板
  • 永康电子商务网站建设公司管理信息系统
  • 手机网站大全1广州注册公司流程及资料
  • 俄语网站模板阿里域名购买网站
  • 上海好的高端网站建设制作app需要什么软件