DEV Community

Cover image for Conditional Statements : Switch / Case
Grant Riordan
Grant Riordan

Posted on • Updated on

Conditional Statements : Switch / Case

Ok, so the last module we learn about the IF / ELSE IF / ELSE functionality within C#, but like I said it can become very messy if you have multiple scenarios you want to test against.

Note: If you have a lot of scenarios, it may be worth asking why so many conditions, and could you break it down into other small components / checks.

So let's take a look at a Switch / Case statement.

The Switch Case Statement

They work in a similar way to an If statement, in that they check against a condition, and execute code if that condition is met. However they are defined, and structured much more differently.

In The Beginning

Originally (prior to C# v.7) it was very simple in its usage. You would pass a value to the switch, and then match it with the available case statements. Depending on that, the matching code block was executed. However things changed with C# 7. You can now perform much more sophisticated expression conditions (cases), which gives them a greater power, when handling multiple scenarios, rather than multiple if / else if / else statments.

Elements of a Switch Case Statement

  • Variable being tested
  • Conditions (Cases)
  • Code to execute
  • Default code (if none of conditions are met), similar to an "Else" statement
  • Break statement (marks the end of the code being executed within switch statement)

Lets take a look at an example and deconstruct.

Alt Text

So we've passed a variable called "Month" to the switch statement, this will be checked for each Case.

We define a case (condition) within parenthesis after the switch declaration. We do this by writing case: and the value for that condition. This could be a string, a number, boolean for example 1, "Morning", true.

Anything after the ":" is classed as code to be executed, if that case is met. We use the break; keyword to exit the switch statement so that no more code, or cases are tested against. Think of it as short circuiting the code , as there's no need to execute the rest.

Changes made in C# 7 - More complex checks can be made

As of C# 7 you were able to make more complex checks and not just checking against a value passed to switch. Before c#7 you were able to write cases against values only. Now, you're now able to do much more complex case checks utilising conditional operators, but they come with some level of complexity.

Lets take a look at what we mean:

Here we use a more complex expression, to test contents of the object.

Alt Text

In this example we use a process called "Casting" , which I'll explain in more detail in a later module. But the general premise is that you're informing the compiler that you want to try and treat the object as a certain type before running the code.

In some of the cases we're saying treat it like a string and then check if the the string starts with "Hel". The power of writing a switch / case like this (using a generic object declaration), is that you can check multiple conditions within one switch statement. As you can see we can check if the value is 5, we can check starts with, contains etc, not just string checks.

It Gets Better C# 8 - More Advanced

It went one step further in C# 8, you can write it in even more shorter hand, using the lambda expression syntax (another thing we'll discuss in future modules) but for now it's just interesting to know / aware you can do it.

Alt Text

As you can see it's much more easier to read, and concise, using the lambda syntax ("=>"). The lambda expression can be thought of as "go to" or "return" this. So we're saying if 1 return "One"
We can also , like before write more expressional conditions and check the value is between ranges etc. Here we're determining which season to return based on current month.

You've probably noticed the underscore. What is this? The default keyword has been replaced with the "_\" discard pattern for brevity. It can then be exploited to run multiple "when" cases as seen in the example above.

Top comments (0)