Switch Statements in C#

Introduction:

This tutorial is on how to use Switch statements in C#.

Switch:

A switch statement allows a single variable or static piece of data to be checked for mulitple conditions, essentially like a long 'else if' statement but a lot more compact and with slightly more flexability.

Case:

To use a switch statement there are a few other keywords we need to learn first, one of which is 'case'. Case is a possible value of the variable or value being 'switched', this must be of the same data type as the value being switched and must be followed by a colon. Break is a keyword used to exit the inner-most loop or statement running, in this case it would be the 'case' statement. Break is used to ensure that the case containing the current 'break' keyword/statement does not 'fall through' in to the next case. Default can be used as a final case just in the event that none of the other cases match the variable or value being 'switched'.

Variable:

Now we are going to write some code. First we will create a string variable which we will use in our switch statement later, we'll name this variable 'switcher' and give it a value of 'Yorkiebar'...
  1. string switcher = "Yorkiebar";

Switch:

Next we create the switch statement, this is the keyword 'switch' followed by the variable/value to be switched in brackets, followed by an enclosed code block...
  1. switch (switcher) {
  2.        
  3. }

Cases:

For the cases we write 'case' followed by the value we are testing for, followed by a colon (':'). Under that line, we type what we want to happen, we'll messagebox something, then we type 'break;' to prevent fall throughs to the following case...
  1. case "Yorkiebar":
  2.         MessageBox.Show("Hi Yorkiebar!");
  3.         break;
  4. case "Josh":
  5.         MessageBox.Show("Something's wrong...");
  6.         break;

Default:

Finally we can write a default code block for the event that none of the above 'case' statements were correct on the currently being 'switched' variable/value. Default must always go last in a switch statement to avoid errors...
  1. default:
  2.         MessageBox.Show("NO!");
  3.         break;

Finished!

Here is the full source code...
  1. using System;
  2. using System.Collections.Generic;
  3. using System.ComponentModel;
  4. using System.Data;
  5. using System.Drawing;
  6. using System.Linq;
  7. using System.Text;
  8. using System.Threading.Tasks;
  9. using System.Windows.Forms;
  10.  
  11. namespace JayPabsSwitchStatements
  12. {
  13.     public partial class Form1 : Form
  14.     {
  15.         public Form1()
  16.         {
  17.             InitializeComponent();
  18.         }
  19.  
  20.         private void Form1_Load(object sender, EventArgs e)
  21.         {
  22.             string switcher = "Yorkiebar";
  23.             switch (switcher)
  24.             {
  25.                 case "Yorkiebar":
  26.                     MessageBox.Show("Hi Yorkiebar!");
  27.                     break;
  28.                 case "Josh":
  29.                     MessageBox.Show("Something's wrong...");
  30.                     break;
  31.                 default:
  32.                     MessageBox.Show("NO!");
  33.                     break;
  34.             }
  35.         }
  36.     }
  37. }

Add new comment