C# switch-case Statement :

switch-case Statement :

The structure switch-case chooses which part of the programming code to execute based on the calculated value of a certain expression.

Syntax :

switch (expression)
case 1:

case 2:


Example :

  1. /* Description: simple switch-case program */
  2. using System;
  3. namespace DemoProgramming
  4. {
  5. class Program
  6. {
  7. static void Main(string[] args)
  8. {
  9. int number=2;
  10. switch (number)
  11. {
  12. case 1:
  13. Console.WriteLine("The 'int number' value is 1");
  14. break;
  15. case 2:
  16. Console.WriteLine("The 'int number' value is 2");
  17. break;
  18. default:
  19. Console.WriteLine("The 'int number' value is Unknown!");
  20. break;
  21. }
  22. Console.ReadKey();
  23. }
  24. }
  25. }

Output :

C Sharp Language simple nested if-else program

Rules for Expressions in Switch :

The switch statement is a clear way to implement selection among many options (namely, a choice among a few alternative ways for executing the code). It requires a selector, which is calculated to a certain value. The selector type could be an integer, char, string or enum. If we want to use for example an array or a float as a selector, it will not work. For noninteger data types, we should use a series of if statements.

Next Update


We create a new YouTube channel (NightHawk) for gaming purpose like online games streaming or games review.

We need some time for make video tutorials (CodeX) so, stay with us and keep supporting and you know already we make a Collaboration with HackIsOn YouTube channel long time ago So, please Subscribe Our Channels

Protection died IPs active, this IP range is not allowed !