Switch Statement in C#
The switch statement is similar to the if statement in that it executes code conditionally based on the value of a test. However, switch enables you to test for multiple values of a test variable in one go, rather than just a single condition. This test is limited to discrete values, rather than clauses such as “ greater than X, ” so its use is slightly different. But it can be a powerful technique.
Example
using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace ConsoleApplication1 { class Program { static void Main(string[] args) { const string myName = "karli"; const string sexyName = "angelina"; const string sillyName = "ploppy"; string name; Console.WriteLine("What is your name?"); name = Console.ReadLine(); switch (name.ToLower()) { case myName: Console.WriteLine("You have the same name as me!"); break; case sexyName: Console.WriteLine("My, what a sexy name you have!"); break; case sillyName: Console.WriteLine("That’s a very silly name."); break; } Console.WriteLine("Hello {0}!", name); Console.ReadKey(); } } }