c# switch example

Example 1: c# switct case

using System;

public class Example
{
   public static void Main()
   {
      int caseSwitch = 1;
      
      switch (caseSwitch)
      {
          case 1:
              Console.WriteLine("Case 1");
              break;
          case 2:
              Console.WriteLine("Case 2");
              break;
          default:
              Console.WriteLine("Default case");
              break;
      }
   }
}
// The example displays the following output:
//       Case 1

Example 2: c# switch statement

string commandName = "start";

switch (commandName)
{
    case "start":
        Console.WriteLine("Starting service...");
        StartService();
        break;
    case "stop":
        Console.WriteLine("Stopping service...");
        StopService();
        break;
    default:
        Console.WriteLine(String.Format("Unknown command: {0}", commandName));
        break;
}

Example 3: c# switch case

using System;
namespace DecisionMaking 
{
   class Program 
   {
      static void Main(string[] args) 
      {
         /* local variable definition */
         char grade = 'B';
         
         switch (grade) 
         {
            case 'A':
               Console.WriteLine("Excellent!");
               break;
            case 'B':
            case 'C':
               Console.WriteLine("Well done");
               break;
            case 'D':
               Console.WriteLine("You passed");
               break;
            case 'F':
               Console.WriteLine("Better try again");
               break;
               default:
            Console.WriteLine("Invalid grade");
               break;
         }
         Console.WriteLine("Your grade is  {0}", grade);
         Console.ReadLine();
      }
   }
}
                       =======OUTPUT========
	Well done
	Your grade is B

Example 4: c# select case

using System;

public class Example
{
   public static void Main()
   {
      int caseSwitch = 1;

      switch (caseSwitch)
      {
          case 1:
              Console.WriteLine("Case 1");
              break;
          case 2:
              Console.WriteLine("Case 2");
              break;
          default:
              Console.WriteLine("Default case");
              break;
      }
   }
}
// The example displays the following output:
//       Case 1

Example 5: switch c#

//This is the supreme way to make a console app with a switch.


using System;

namespace Example
{
    class Program
    {
        static void Main(string[] args)
        {
          	//Greats the user.
          	Console.WriteLine("Hello!");
            Console.WriteLine();
          	
          	//An infinite loop for continues command reading.
            while (true)
            {
              	//Asks for input on a line starting with >>.
                Console.Write(">> ");
                input = Console.ReadLine();
              	
              	//Checks if the user wants to exit.
                if (input == "Close") break;
              
              	//Gets the first word of the line and puts it in command.
                var command = input.Split(' ')[0];
              
              	//sets up a switch for all the possible commands
                switch (command)
                {
                    case "log":
                    	//This is entered if log is submited into the Console.
                       	Console.WriteLine("Not implemented");
                        break;
                    case "":
                        //Leaves the user alone. This is needed because otherwise it would
                    	//cosnider "" as the defualt and print Unknown command.
                        break;
                    default:
                        Console.WriteLine("Unknown command.");
                        Console.WriteLine();
                        break;
                }
            }
        }
    }
}

Example 6: c# switch example

switch (iGender)
            {
                case 1:
                    LabelOne.Text = "Male";
                    break;

                case 2:
                    LabelOne.Text = "Female";
                    break;