首页  >  文章  >  后端开发  >  C# 模式匹配

C# 模式匹配

PHPz
PHPz原创
2024-09-03 15:32:47406浏览

C# 中用于匹配任何数据或任何对象的功能称为模式匹配,此模式匹配是使用表达式 is 和 switch 语句执行的,其中 is 表达式检查数据或对象是否与指定的类型兼容或not 和 switch 语句用于匹配 C# 中的不同模式。通过在C#中使用表达式is和switch语句进行模式匹配,可以改变应用程序的编写方式,使其更具可读性,更易于维护,并且易于理解。

C# 模式匹配的方法

C# 中有两种模式匹配方法。他们是:

1.是表达式

is 表达式用于检查数据或对象与指定类型的兼容性。

示例#1

要演示的 C# 程序是程序中的表达式。

代码:

using System;
//a namespace called program is defined
namespace program
{
//a class called subject is defined in which a subject is assigned to a string variable
class Subject
{
public string SubName{ get; set; } = "C Sharp";
}
//a class called check is defined
class check
{
//main method is called
public static void Main(string[] args)
{
//an instance of the subject class is created
Subject sub = new Subject();
//is expression is used to check if the instance of the subject class is compatible with the type of the value assigned to the string variable in subject class
if(sub is Subject)
{
Console.WriteLine(sub.SubName);
}
}
}
}

输出:

C# 模式匹配

说明:在上面的程序中,定义了一个名为program的命名空间。然后定义一个名为“Subject”的类,其中将主题名称分配给一个字符串变量。然后定义一个名为 check 的类,在该类中调用 main 方法。然后创建主题类的实例。然后表达式用于检查新创建的主题类实例是否与分配给类主题中字符串变量的值的类型兼容。如果兼容,则主题名称将显示为输出。程序的输出如上面的快照所示。

示例#2

要演示的 C# 程序是程序中的表达式。

代码:

using System;
//a namespace called program is defined
namespace program
{
//a class called Writer is defined in which a name of the writer is assigned to a string variable
class Writer
{
public string WriterName{ get; set; } = "ShobhaShivakumar";
}
//a class called check is defined
class check
{
//main method is called
public static void Main(string[] args)
{
//an instance of the Writer class is created
Writer write = new Writer();
//is expression is used to check if the instance of the Writer class is compatible with the type of the value assigned to the string variable in Writer class
if(write is Writer)
{
Console.WriteLine(write.WriterName);
}
}
}
}

输出:

C# 模式匹配

说明:在上面的程序中,定义了一个名为program的命名空间。然后定义一个名为 Writer 的类,其中将编写者的名称分配给一个字符串变量。然后定义一个名为 check 的类,在其中调用 main 方法。然后创建 Writer 类的实例。然后表达式用于检查新创建的 Writer 类实例是否与分配给 Writer 类中字符串变量的值的类型兼容。如果兼容,则作者的姓名将显示为输出。程序的输出如上面的快照所示。

2. Switch 语句

switch 语句用于在 C# 中匹配不同的模式。

示例#1

在程序中演示 Switch 语句的 C# 程序。

代码:

using System;
//a class called check is defined
class check
{
//main method is called
public static void Main()
{
//a name is assigned to a string variable
string val = "Shobha_Shivakumar";
//switch statement is used to switch between the values that is assigned to the string variable and anything else
switch (val)
{
case "Shobha_Shivakumar":
Console.WriteLine("The assigned value is Shobha_Shivakumar");
break;
case "not_assigned":
Console.WriteLine("The assigned value is not_assigned");
break;
}
}
}

输出:

C# 模式匹配

说明:在上面的程序中,定义了一个名为 check 的类。然后调用 main 方法,其中将名称分配给字符串变量。然后 switch 语句用于在分配给字符串变量的值和其他任何值之间切换。如果分配给字符串变量的名称是 switch case,则显示相应的输出。同样,如果是其他内容,则显示相应的输出。程序的输出如上面的快照所示。

示例#2

在程序中演示 Switch 语句的 C# 程序:

代码:

using System;
//a class check is defined
class Check
{
//main method is called
static void Main()
{
while(true)
{
Console.WriteLine("Type any alphabet between A and Z");
try
{
//a string is expected as the input by the user
string r = Console.ReadLine();
//switch statement is used to parse the input given by the user and display the output accordingly
switch(r)
{
case "A":
{
Console.WriteLine("This is Alphabet A");
break;
}
case "B":
{
Console.WriteLine("This is alphabet B");
break;
}
default:
{
Console.WriteLine("This is something other than Alphabets A and B");
break;
}
}
}
catch
{
}
}
}
}

输出:

C# 模式匹配

说明:在上面的程序中,定义了一个名为check的类。然后调用 main 方法,其中字母表 A 到 Z 之间的字符串预计作为用户的输入。然后 switch 语句用于解析用户提供的输入并相应地显示输出。如果输入是 A 和 B 以外的任何字母,则输出中也会显示相同的字母。上述程序的输出如上面的快照所示。

结论

在本教程中,我们通过示例及其输出来了解模式匹配的定义和方法,从而了解 C# 中模式匹配的概念。

推荐文章

这是 C# 模式匹配的指南。在这里,我们讨论 C# 模式匹配简介及其方法以及示例和代码实现。您还可以浏览我们其他推荐的文章以了解更多信息 –

  1. C# 中的随机数生成器
  2. Java 中的静态构造函数
  3. C# 中的 TextWriter
  4. C# 中的静态构造函数

以上是C# 模式匹配的详细内容。更多信息请关注PHP中文网其他相关文章!

声明:
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn