C#
 Computer >> コンピューター >  >> プログラミング >> C#

C#でネストされたswitchステートメントをどのように使用しますか?


C#では、あるswitchステートメントを別のswitchステートメント内で使用できます。外部スイッチのステートメントシーケンスの一部としてスイッチを持つことができます。内部スイッチと外部スイッチのケース定数に共通の値が含まれていても、競合は発生しません。

構文は次のとおりです。

switch(ch1) {
   case 'A':
   Console.WriteLine("This A is part of outer switch" );
   switch(ch2) {
      case 'A':
      Console.WriteLine("This A is part of inner switch" );
      break;
      case 'B': /* inner B case code */
   }
   break;
   case 'B': /* outer B case code */
}

以下は、C#でネストされたswitchステートメントの例です。

switch (a) {
   case 100:
   Console.WriteLine("This is part of outer switch ");
   switch (b) {
      case 200:
      Console.WriteLine("This is part of inner switch ");
      break;
   }
   break;
}

完全な例を見てみましょう。

using System;
namespace Demo {
   class Program {
      static void Main(string[] args) {
         int a = 100;
         int b = 200;
         switch (a) {
            case 100:
            Console.WriteLine("This is part of outer switch ");
            switch (b) {
               case 200:
               Console.WriteLine("This is part of inner switch ");
               break;
            }
            break;
         }
         Console.WriteLine("Exact value of a is : {0}", a);
         Console.WriteLine("Exact value of b is : {0}", b);
         Console.ReadLine();
      }
   }
}

出力

This is part of outer switch
This is part of inner switch
Exact value of a is : 100
Exact value of b is : 200

  1. アンドロイドリストビューでネストされた配列を使用する方法は?

    この例は、Androidリストビューでネストされた配列を使用する方法について示しています。 ステップ1-AndroidStudioで新しいプロジェクトを作成し、[ファイル]⇒[新しいプロジェクト]に移動して、新しいプロジェクトを作成するために必要なすべての詳細を入力します。 ステップ2-次のコードをres/layout / activity_main.xmlに追加します <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="https://sc

  2. サムスンスマートスイッチの使い方

    Samsung Smart Switchは、あるモバイルデバイスから別のモバイルデバイスへのファイル、ビデオ、連絡先、写真、またはアプリケーションの転送を可能にすることが知られています。また、電話間での転送中にコンテンツのバックアップと復元を行うこともできます。さらに、ソフトウェアアップデートのインストールや電子メールの同期にも使用できます。 このページでは、データの転送やコンテンツのバックアップと復元に使用されるプログラムとして、SamsungSmartSwitchを使用する方法を紹介します。ページを下にスクロールし続けると、このすばらしいプログラムの使用法について学ぶことができます。