c# switch case enum
enum Menu
{
Students_Detail = 1,
Display = 2,
Exit = 3
}
static void Main(string[] args)
{
Console.WriteLine("Please select a menu option");
Console.WriteLine("1. Students Detail");
Console.WriteLine("2. Display");
Console.WriteLine("3. Exit");
int pick = Convert.ToInt32(Console.ReadLine());
Menu menuChoice = new Menu();
menuChoice = (Menu)pick;
switch (pick)
{
case 1:
break;
case 2:
break;
case 3:
break;
default:
break;
}
}
3.44
9
enum Country { Spain, USA, Japan };
static void Main(string[] args)
{
Country country;
Console.WriteLine("Enter the number of country\n 1.Spain \n 2.The USA \n 3.Japan");
string input = Console.ReadLine();
bool sucess = Enum.TryParse<Country>(input, out country);
if (!sucess)
{
Console.WriteLine("entry {0} is not a valid country", input);
return;
}
switch (country)
{
case Country.Spain:
Console.WriteLine("Its in Europe");
break;
case Country.USA:
Console.WriteLine("Its in North America");
break;
case Country.Japan:
Console.WriteLine("Its in Asia");
break;
}
Console.ReadKey();
}
public static RGBColor FromRainbow(Rainbow colorBand) =>
colorBand switch
{
Rainbow.Red => new RGBColor(0xFF, 0x00, 0x00),
Rainbow.Orange => new RGBColor(0xFF, 0x7F, 0x00),
Rainbow.Yellow => new RGBColor(0xFF, 0xFF, 0x00),
Rainbow.Green => new RGBColor(0x00, 0xFF, 0x00),
Rainbow.Blue => new RGBColor(0x00, 0x00, 0xFF),
Rainbow.Indigo => new RGBColor(0x4B, 0x00, 0x82),
Rainbow.Violet => new RGBColor(0x94, 0x00, 0xD3),
_ => throw new ArgumentException(message: "invalid enum value", paramName: nameof(colorBand)),
};
public static RGBColor FromRainbowClassic(Rainbow colorBand)
{
switch (colorBand)
{
case Rainbow.Red:
return new RGBColor(0xFF, 0x00, 0x00);
case Rainbow.Orange:
return new RGBColor(0xFF, 0x7F, 0x00);
case Rainbow.Yellow:
return new RGBColor(0xFF, 0xFF, 0x00);
case Rainbow.Green:
return new RGBColor(0x00, 0xFF, 0x00);
case Rainbow.Blue:
return new RGBColor(0x00, 0x00, 0xFF);
case Rainbow.Indigo:
return new RGBColor(0x4B, 0x00, 0x82);
case Rainbow.Violet:
return new RGBColor(0x94, 0x00, 0xD3);
default:
throw new ArgumentException(message: "invalid enum value", paramName: nameof(colorBand));
};
}
Thank you!
9
0
4.25
8
public enum Operator
{
PLUS, MINUS, MULTIPLY, DIVIDE
}
switch(op)
{
case Operator.PLUS:
{
// your code
// for plus operator
break;
}
case Operator.MULTIPLY:
{
// your code
// for MULTIPLY operator
break;
}
default: break;
}
Thank you!
8
0
5
1
public class Example
{
// Button click event
public void Click(object sender, RoutedEventArgs e)
{
if (sender is Button handler)
{
switch (handler.Tag.ToString())
{
case string tag when tag.StartsWith("Example"):
// your code
break;
default:
break;
}
}
}
}
Thank you!
1
0
Are there any code examples left?
New code examples in category C#
-
C# 2022-03-27 22:40:23 c# movement script
-
C# 2022-03-27 22:15:17 Delete last modification on EntityFramework Core
-
C# 2022-03-27 22:15:02 file.create folder c#
-
C# 2022-03-27 19:25:04 Error inflating class android.support.constraint.ConstraintLayout
-
C# 2022-03-27 19:15:02 top down shooting in unity 2D
-
C# 2022-03-27 18:50:02 c# remove invalid directory characters
-
C# 2022-03-27 18:30:07 how to use K2 games Games parallax background
-
C# 2022-03-27 18:25:10 c# replace crlf
-
C# 2022-03-27 18:25:09 keycode for left mouse click unity