A Simple Switch Case Example

A simple example how a switch case works.


using System;
class SwitchCaseExample
{
int x;
public static void Main()
{
SwitchCaseExample sc = new SwitchCaseExample();
sc.Accept();
sc.Display();
}
public void Display()
{
switch (x)
{
case 1:
//opens Notepad
System.Diagnostics.Process.Start("Notepad");
break;
case 2:
//opens Calculator
System.Diagnostics.Process.Start("Calc");
break;
case 3:
//opens Microsoft Paint
System.Diagnostics.Process.Start("Mspaint");
break;
default:
Console.WriteLine("Please Enter Choice between 1 to 3,Try Again!!!");
break;
}
}
public void Accept()
{
Console.WriteLine("A Simple Switch Case Example");
Console.WriteLine("\n1.Notepad\n2.Calculator\n3.MsPaint\n");

Console.Write("Enter Choice to Perform Operation: ");
x = Convert.ToInt32(Console.ReadLine());

}
}

Try Out this code.


Comments

Author: Sanjay20 Jul 2010 Member Level: Silver   Points : 2

Hi Nikhil,

namespace switchcase
{
class SwitchCase
{
static void Main(string[] args)
{
string input;
Console.WriteLine("Enter the month:");
input = Console.ReadLine().ToUpper();

switch (input)
{
case "JANUARY":
case "MARCH":
case "MAY":
case "JULY":
case "AUGUST":
case "OCTOBER":
case "DECEMBER":

Console.WriteLine("This month has 31 day's");
break;

case "APRIL":
case "JUNE":
case "SEPTEMBER":
case "NOVEMBER":
Console.WriteLine("This month has 30 days");
break;

case "FEBRUARY":
Console.WriteLine("This month has 28 days in a leap year and 29 days in leap year");
break;

default:
Console.WriteLine("Incorrect choice.....");
break;
}

}
}
}

-Sanjay D Tank.



  • Do not include your name, "with regards" etc in the comment. Write detailed comment, relevant to the topic.
  • No HTML formatting and links to other web sites are allowed.
  • This is a strictly moderated site. Absolutely no spam allowed.
  • Name:
    Email: