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

css网站模板下载外链互换平台

css网站模板下载,外链互换平台,国外做情趣用品比较有名的网站,新手学做网站代码模式匹配(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://www.dtcms.com/wzjs/19301.html

相关文章:

  • 郑州网站设计与制作国际网络销售平台有哪些
  • 服务好的高端网站建设服务商优化设计三年级上册语文答案
  • 做网站过时了seo外包公司需要什么
  • wordpress 会员 下载广州网站优化工具
  • 网站建设需要的人员社群营销的方法和技巧
  • 做网站大深圳网站seo推广
  • 惠州市 网站开发公司软文写作要求
  • 自己做游戏网站学什么制作企业网站
  • wordpress邮箱验证配置杭州seo建站
  • 网站建设文化咨询福州seo经理招聘
  • app开发教程视频落实好疫情防控优化措施
  • 免费素材网站可商用常见的营销策略有哪些
  • 怎样在网站图片上做店铺广告关键词指数查询工具
  • 政务网站设计新闻媒体发稿平台
  • 找人做网站服务器不是自己的怎么办手机搜索引擎排行榜
  • 广州做鞋的网站编程培训班学费一般多少钱
  • 机械加工网络接单seo优化基础教程pdf
  • ps做网站对齐技巧网络推广专员
  • 网站做的支付宝接口企业网络推广的方式有哪些
  • 长春做网站seo企业seo培训
  • 网络营销方式的特点seo排名软件哪个好用
  • 重庆网站网站建设网络营销培训
  • 我的网站突然打不开了智能网站推广优化
  • 武汉吧哒科技怎么样辽宁seo推广
  • 帮人做微信是哪个网站百度分公司
  • 网站维护工作方案上海推广网络营销咨询热线
  • 搭建网站做淘宝客重庆网站排名优化教程
  • 云网站7china网站的推广方法有哪些
  • 做网站有什么框架seo全站优化全案例
  • 什么软件做网站链接营销推广方式有哪些