switch statement c# example
int number = 2;
switch(number)
{
case 1:
Console.WriteLine("Number is 1");
break;
case 2:
Console.WriteLine("Number is 2");
break;
}
switch statement c# example
int number = 2;
switch(number)
{
case 1:
Console.WriteLine("Number is 1");
break;
case 2:
Console.WriteLine("Number is 2");
break;
}
c# switch when
/*
Why use many "if" statements if you can just use a switch
and clean alot of your code!
Below are two ways to make your life alot easier!
*/
// Using a traditional switch statement
string test = "1";
switch (test)
{
case "*":
Console.WriteLine("test");
break;
case "Something else":
Console.WriteLine("test1");
break;
case string when test != "*":
Console.WriteLine("test2");
break;
default:
Console.WriteLine("default");
break;
}
// Using a switch expression
// This obviously results in much cleaner code!
string result = test switch
{
"*" => "test",
"test" => "test1",
string when test != "*" => "test2",
_ => "default" // In switch expressions the _ is the same as default
};
Console.WriteLine(result);
Copyright © 2021 Codeinu
Forgot your account's password or having trouble logging into your Account? Don't worry, we'll help you to get back your account. Enter your email address and we'll send you a recovery link to reset your password. If you are experiencing problems resetting your password contact us