Loading ...

C# Switch Statement | CodeAsp.Net

C# Switch Statement

 /5
0 (0votes)

When you need to validate a small amount of logical evaluations the "if / then" statement will usually suffice.  However, when you need to validate more and more logical evaluations a switch statement is going to be more managable.

Here is the syntax for a C# switch statement:

switch (value)
{
     case 1:
          //do stuff
          break;

     case 2:
          //do stuff
          break/jump

     default:
          //do stuff
          break/jump
}

...and here is an example of how you can use it to validate a string value

string str = "switch statement";

switch (str)
{
     case "switch statement":
          //do stuff
          break;

     case "another statement":
          //do stuff
          break/jump

     default:
          //do stuff
          break/jump
}

And there you have it. The C# switch statement. A great little piece of code to help you validate logical evaluations.

Comments (no comments yet)

  • :*
  • :*
  • :
 *

Top Posts