From the course: Advanced C#: Functional Programming Patterns

Unlock this course with a free trial

Join today to access over 24,500 courses taught by industry experts.

Rewrite SWITCH statement with expression

Rewrite SWITCH statement with expression - C# Tutorial

From the course: Advanced C#: Functional Programming Patterns

Rewrite SWITCH statement with expression

In the last video, we saw the if statement that evaluates a Boolean expression and branches the code based on the result. In that example, I didn't do much branching because there was no else clause in the code. Another type of branching code that we use in C# is through switch statements. For this example, I'll be switching on this enumeration value called StandardColors. It's got values like red, orange, yellow, and so on. And then I have a method up here that is passing in the current enum value. And then it's going to do some statement-based code here. So this is classic old-school switch statement. I start off by declaring a variable on line 70. It's called hexString and I'm assigning a string empty. The idea here is that we pass in the color enumeration, and then the code returns a hexadecimal string value for that color. You'll notice I'm declaring a variable, and there's an underline on hexString, which tells me that the value is being changed. And you can see it's being…

Contents