How to use C# switch case statements

C# switch case statements

The C# switch statement allows you to choose from many statements based on multiple selections by passing control to one of the case statements within its body. The switch statement executes the case corresponding to the value of the expression . The switch statement can include any number of case instances.

Related Topics:

If..Else statement , Enum Switch..Case , String to DateTime , Character Encoding , Autocomplete TextBox , Exception Vs Error
  1. expression : An integral or string type expression.

  2. jump-statement : A jump statement that transfers control out of the case body.

String Switch

C# String Switch Case

The C# language allows you to switch on a string variable. The switch statement compares the String objects in its expression with the expressions associated with each case label as if it were using the String.equals method. Currently the switch statement is case-sensitive. It would be nice to be able to specify the StringComparison to use for switching on strings. String in switch case make code more readable by removing the multiple if-else-if chained conditions.

C# Switch Case integer

If any of the expression passed to switch case does not match with case statement the control will go to default: statement . If there is no default: statement control will go outside of the switch statement. The following C# program shows how to int values work with Switch..Case statement.

using System;
using System.Windows.Forms;

namespace WindowsApplication1
    public partial class Form1 : Form
        public Form1()

        private void button1_Click(object sender, EventArgs e)
            int val = 5;
            switch (val)
                case 1:
                    MessageBox.Show("The day is - Sunday");
                case 2:
                    MessageBox.Show("The day is - Monday");
                case 3:
                    MessageBox.Show("The day is - Tuesday");
                case 4:
                    MessageBox.Show("The day is - wednesday");
                case 5:
                    MessageBox.Show("The day is - Thursday");
                case 6:
                    MessageBox.Show("The day is - Friday");
                case 7:
                    MessageBox.Show("The day is - Saturday");
                    MessageBox.Show("Out of range !!");
} (C) 2021    Founded by raps mk
All Rights Reserved. All other trademarks are property of their respective owners.
SiteMap  | Terms  | About