switch on csharp code example

Example 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;
                }
            }
        }
    }
}

Example 2: c# switch

using System;

namespace SwitchStatement
{
  class Program
  {
    static void Main(string[] args)
    {
      Console.WriteLine("Please write a movie genre.");
      string genre = Console.ReadLine();
      switch (genre)
      {
        case "Drama":
        Console.WriteLine("Citizen Kane");
        break;
        case "Comedy":
        Console.WriteLine("Duck Soup");
        break;
        case "Adventure":
        Console.WriteLine("King Kong");
        break;
        case "Horror":
        Console.WriteLine("Psycho");
        break;
        case "Science Fiction":
        Console.WriteLine("2001: A Space Odyssey");
        break;
        default:
        Console.WriteLine("No movie found.");
        break;
      }
    }
  }
}