Evaluating an Expression for Multiple Values Using switch

At times, the if construct isn't capable of handling a decision situation without a lot of extra work. One such situation is when you need to perform different actions based on numerous possible values of an expression, not just true or false. For instance, suppose that you wanted to perform actions based on a user's profession. The following shows what you might create using if:

if (strProfession =="programmer")
   ...
else if (strProfession =="teacher")
   ...
else if (strProfession =="accountant")
   ...
else
   ...

As you can see, this structure can be a bit hard to read. If the number of supported professions increases, this type of construction will get harder to read and debug. In addition, ...

Get Sams Teach Yourself Microsoft® Visual C#™ .NET in 24 Hours now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.