C# Switch Statement

If you would like to perform a series of specific checks, switch...case is present in C# only for this. The general structure of the switch...case statement is:

switch(integral or string expression)
{
          case constant-expression:
              // statements
           breaking or jump statement
          // another case blocks
           ...
          default:
              statements
          breaking or jump statement
}

There are some details to recollect when using the switch...case statement in C#:

  • You'll use either integer (enumeration) or strings during a switch statement
  • The expression following the case must be constant. it's illegal to use a variable after the case:
  • A colon: is employed after the case statement and not a semicolon ;
  • you'll use multiple statements under single case and default statements:
  • The end of the case and default statements is marked with a break (or goto) statement. We don’t use {} brackets to mark the block in the switch...case as we usually neutralize C#
  • C# doesn't allow fall-through. So, you'll ’t leave case or default without a break statement (as you can in Java or C/C++). The compiler will detect and complain about the utilization of fall-through within the switch...case statement.
  • The break statement transfers the execution control out of the present block.
  • Statements under default are going to be executed if and as long as all the case checks fail.
  • it's not necessary to put default at the top of the switch...case statement. you'll even place the default block before the primary case or in-between cases; the default will work an equivalent no matter its position.

Rules for Expressions in Switch

The switch statement may be a clear group of statements to implementing selection among many options (namely, a choice among a couple of other ways for executing the code). It requires a selector, which is calculated to a particular value. The selector type might be an integer number, char, string, or enum. If we would like to use for example an array or a float as a selector, it'll not work. For non-integer data types, we should always use a series of if statements.

Example:

int number = 6;
switch (number)
{
	case 1: 
		Console.WriteLine("this is case 1"); 
		break;
	case 2:
		Console.WriteLine("this is case 2"); 
		break;
	case 3: 
		Console.WriteLine("this is case 3"); 
		break;
	case 4: 
		Console.WriteLine("this is case 4"); 
		break;
	case 5: 
		Console.WriteLine("this is case 5"); 
		break;
	case 6: 
		Console.WriteLine("this is case 6"); 
		break;
	case 7: 
		Console.WriteLine("this is case 7"); 
		break;
	case 8: 
		Console.WriteLine("this is case 8"); 
		break;
	case 9: 
		Console.WriteLine("this is case 9"); 
		break;
	case 10: 
		Console.WriteLine("this is case 10"); 
		break;
	default: 
		Console.WriteLine("Unknown number!"); 
		break;
}

Output:

this is case 6

In the above example, we are implementing multiple statements by using case statements with the break.  First of all integer value of the selector will be calculated that is 6. Then it will be compared with all case statement integer numbers and then print the related output if no match is found then the default statement will be called.

Nested Switch

A switch statement under the switch statement is known as a nested switch statement

Example:

           int number = 2;

			switch (number)
			{
				
				case 1:
					Console.WriteLine(11);
					break;
				case 2:
					switch(number + 1)
                    {
						case 3:
							Console.WriteLine(number + 1);
							break;
                    }
					switch(number + 2)
                    {
						case 4:
							Console.WriteLine(number + 2);
							break;
                    }
					break;
				default:
					Console.WriteLine(100);
					break;
			}

Output:

3
4