switch case with condition c# code 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;
}
}
}
Example 2: switch c#
switch(expression)
{
case x:
break;
case y:
break;
default:
break;
}
Example 3: 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;
}
}
}
}
Example 4: c# switch
int randomNumber = rnd.Next(1, 2);
switch (randomNumber)
{
case 1:
Console.WriteLine("randomNumber = 1!");
break;
case 2:
Console.WriteLine("randomNumber = 2!");
break;
default:
Console.WriteLine("something went wrong");
break;
}