Answers for "how to use switch statement in c#"

C#
17

c# switch

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
Posted by: Guest on January-16-2020
1

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;
                }
            }
        }
    }
}
Posted by: Guest on October-29-2020
1

how to use switch statement in c#

/* Switch Statements are a cleaner way to write if and else statements like this: */

var randomNum = Mathf.Random.Range(0, 5);

// ofcourse this way is so dumb but its just for explaining
if (randomNum == 0) print("The correct number is 0");
else if (randomNum == 1) print("The correct number is 1");
else if (randomNum == 2) print("The correct number is 2");
else if (randomNum == 3) print("The correct number is 3");
else if (randomNum == 4) print("The correct number is 4");
else if (randomNum == 5) print("The correct number is 5");

// Instead of writing if and else statements like this we can use switch statements like this:

switch(randomNum)
{
  case 0:
    print("correct number is 0");
    break; // we must include a break statement at the end of every case.
    
  case 1:
    print("correct number is 1");
    break;
    
  case 2:
    print("correct number is 2");
    break;
    
  case 3:
    print("correct number is 3");
    break;
    
  case 4:
    print("correct number is 4");
    break;
    
  case 5:
    print("correct number is 5");
    break;
}
Posted by: Guest on June-25-2021
1

c# switch

switch (caseSwitch)
{
  case 1:
    Console.WriteLine("Case 1");
    break;
  case 2:
    Console.WriteLine("Case 2");
    break;
  default:
    Console.WriteLine("Default case");
    break;
}
Posted by: Guest on April-09-2021

Code answers related to "how to use switch statement in c#"

C# Answers by Framework

Browse Popular Code Answers by Language