O'Reilly logo

C# 5.0 Unleashed by Bart De Smet

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

The switch Statement

It’s a tradition to introduce the switch statement using the if statement first. Consider the following fragment:

Image GetBackgroundForColorScheme(Color color){    if (color == Color.Red)        return Image.FromFile("Roses.png");    else if (color == Color.Green)        return Image.FromFile("Grass.png");    else if (color == Color.Blue)        return Image.FromFile("Sky.png");    else        throw new InvalidOperationException("Expected primary color.");}

I’m using some System.Drawing types such as Color and Image in the preceding fragment, but ignore this detail for just a second and focus on what makes this fragment tick.

Programmers often refer to this if statement pattern as a multiway ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required