How To Write a Select Case Statement in C#

A select statement is one of those logical statements used in programming. These statements require either a true or false answer in order for the program to decide what to do or where to go next. You can easily identify this type statement in simply because it uses the If and Then logical sequence. In C#, however, the code is a bit more complicated. Here are the basics on how to write such a statement using C#.

  • This is a mere example of how to write a select case statement. Bear in mind that the sample code provided is not the same code to use for your own select case statement. You can follow the standard code format, though.
  • Boot up your PC and double-click on the Visual Studio icon to run the program.
  • Open up a new project by selecting File and New Project in the menu.
  • Now, when selecting New Project, make sure that you choose Visual Basic C++. This can be found on the Other Languages tab. Bear in mind that you can use the standard language if you feel that the syntax there is easier for you. Coding in Visual Basic C++ can be rather meticulous as compared to
  • Select a window application by navigating to Templates. The option will be available there. Simply click on it and select OK to apply the chosen configuration. This will open up template on the blank page you have open.
  • Insert the control for the button on the window application. The control is where the code will be inserted, where the select case statement will be encoded. To bring up the control, you will need to double-click the button. Bear in mind that the control will have no select case statement in it yet. It is your responsibility to put that in.
  • As you open the control window for the button, you will see around 3 lines in it already. Pay attention to the line with the label “private void button1_Click{object sender, EventArgs e}. This is the line that will signify where to place the select case statement. Place it right below that line.
  • Type in this sample code:

    int K = 0;

    switch (k)

        case 3:
            MessageBox.Show(“K is 3”) ;
        case 6:    
            MessageBox.Show(“K is 6”) ;
            MessageBox.Show(“I do not know what K is!”) ;


  • Save the code you have encoded and test it out. To run the code, simply hit F5 on the keyboard. You should immediately see the results of your select case statement by clicking on the button.
  • Now, try to change the variables of the statement to get a different result.
  • You can even change the template that you are using to one that has a field where you can type in another variable that will refer to the code you specified.

You have successfully written a select case statement in C#. The process is pretty much the same in but the syntax is not. Hence, try to determine what will be easier for you. Some people feel more comfy using C# while others find’s code syntax a lot friendlier.


Share this article!

Follow us!

Find more helpful articles: