C# パターン マッチング

PHPz
リリース: 2024-09-03 15:32:47
オリジナル
410 人が閲覧しました

データまたはオブジェクトの一致に使用される C# の機能はパターン マッチングと呼ばれ、このパターン マッチングは is 式と switch ステートメントを使用して実行されます。is 式は、データまたはオブジェクトに指定された型との互換性があるかどうかをチェックします。 not および switch ステートメントは、C# のさまざまなパターンと一致させるために使用されます。 C# のパターン マッチングに is ステートメントと switch ステートメントを使用することで、アプリケーションの記述方法を、より読みやすく、保守しやすく、理解しやすいものに変更できます。

C# パターン マッチングの方法

C# のパターン マッチングには 2 つの方法があります。それらは次のとおりです:

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 という名前空間が定義されています。次に、ライターという名前のクラスが定義され、ライターの名前が文字列変数に割り当てられます。次に、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 中国語 Web サイトの他の関連記事を参照してください。

関連ラベル:
ソース:php
このウェブサイトの声明
この記事の内容はネチズンが自主的に寄稿したものであり、著作権は原著者に帰属します。このサイトは、それに相当する法的責任を負いません。盗作または侵害の疑いのあるコンテンツを見つけた場合は、admin@php.cn までご連絡ください。
最新の問題
人気のチュートリアル
詳細>
最新のダウンロード
詳細>
ウェブエフェクト
公式サイト
サイト素材
フロントエンドテンプレート
私たちについて 免責事項 Sitemap
PHP中国語ウェブサイト:福祉オンライン PHP トレーニング,PHP 学習者の迅速な成長を支援します!