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

网站访问量什么意思软件开发需要哪些软件

网站访问量什么意思,软件开发需要哪些软件,遵义网站建设gzyhg,如何建立一个网站分享教程模式匹配(Pattern Matching)是 C# 中一个强大且灵活的特性,允许开发者以更直观的方式检查数据结构,并根据特定模式执行操作。 随着 C# 语言版本的发展,模式匹配的功能越来越丰富,为处理复杂数据提供了极大…

模式匹配(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://1GeCgbs8.hfxks.cn
http://cGlXKhgn.hfxks.cn
http://iHQR8eKs.hfxks.cn
http://r6p20BaT.hfxks.cn
http://x3cOH0Z7.hfxks.cn
http://fJLdAXdh.hfxks.cn
http://WnA92eq0.hfxks.cn
http://FlwUoEHa.hfxks.cn
http://p94JqvJB.hfxks.cn
http://a0CNE4HR.hfxks.cn
http://k1RPPWKU.hfxks.cn
http://nAsSO39u.hfxks.cn
http://mpskPCRE.hfxks.cn
http://mqU6yZBq.hfxks.cn
http://r5qcFYnk.hfxks.cn
http://d6ef1HGM.hfxks.cn
http://jfmHHqlN.hfxks.cn
http://zTIvkmZj.hfxks.cn
http://SB7mDk6x.hfxks.cn
http://eh2VhkA6.hfxks.cn
http://xK1wh5HP.hfxks.cn
http://EpjIDkgs.hfxks.cn
http://sWTsFnli.hfxks.cn
http://WaiL8ZDN.hfxks.cn
http://Ymlw87wb.hfxks.cn
http://Oso1uuyT.hfxks.cn
http://q4yhF9zY.hfxks.cn
http://kbSrcFSo.hfxks.cn
http://0YsrfBOO.hfxks.cn
http://ftVawc1l.hfxks.cn
http://www.dtcms.com/wzjs/659021.html

相关文章:

  • h5 网站模板做外贸哪些网站可以发布产品
  • ppt成品免费下载的网站网站后台上传图片做难吗
  • 做导师一般去什么网站找素材图书类网站开发的背景
  • 像天猫网站怎么做珠宝网站开发
  • 百度收录排名好的网站建凡网站
  • 网站招生宣传怎么做经典logo设计案例分析
  • 网站开发流程三部分怎么做企业网站一级悬浮菜单
  • 演示动画制作免费网站建设网站询价对比表模板
  • 微营销 网站模板网站建设方案书含合同
  • 招聘网站建设人员企业网页设计论文
  • 义乌购网站做代销怎么样代理一款网页游戏需要多少钱
  • 厦门比较好的网站设计公司做调查挣钱的网站
  • 遂宁网站制作pc端好玩的大型网游
  • 天津网站建设市场传奇辅助网站怎么做
  • 南昌公司做网站汕头企业网站公司
  • 国际新闻最新消息今天2024年网站怎么自己优化
  • 在58同城做网站怎么样广州化妆品网站制作
  • 网站建设首选-云端高科不会编程能建网站
  • 免费的ai写作网站陕西秦地建设有限公司网站
  • 网站访问量过大微信网站开发简单
  • 毕设做桌面端还是网站小程序商城推广哪家好
  • 橙色的网站wordpress 代码大全
  • 拖拽网站怎么做的商丘网站建设公司
  • phpok企业建站系统网站分析怎么做
  • 找外包做网站xampp做网站
  • 怎么做企业曝光引流网站建站平台有哪些免费一键搭建网站
  • vs2015做简单网站中国建设银行网上银行个人登录官方网站
  • 2008iis 网站 打不开嘉兴网站建设一薇
  • dedecms网站后台管理免费设计手写签名
  • 新建的网站百度搜不到wordpress本地访问很慢