C# Switch Examples
Following examples show switch statement. You can debug examples online.
Switch with Default Section
The following example shows a simple switch statement that has three switch sections. First two sections start with case label followed by constant value. If a value passed to the switch statement matches any case label constant the specified switch section is executed, otherwise the default section is executed. One switch section can contain more than one statements.
C# Switch |
---|
int i = 1;
switch (i)
{
case 1:
Console.WriteLine("One");
break;
case 2:
Console.WriteLine("Two");
Console.WriteLine("Two");
break;
default:
Console.WriteLine("Other");
break;
}
|
Output |
---|
One
|
C# Switch |
---|
int i = 2;
switch (i)
{
case 1:
Console.WriteLine("One");
break;
case 2:
Console.WriteLine("Two");
Console.WriteLine("Two");
break;
default:
Console.WriteLine("Other");
break;
}
|
Output |
---|
Two
Two
|
C# Switch |
---|
int i = 3;
switch (i)
{
case 1:
Console.WriteLine("One");
break;
case 2:
Console.WriteLine("Two");
Console.WriteLine("Two");
break;
default:
Console.WriteLine("Other");
break;
}
|
Output |
---|
Other
|
Switch Without Default Section
If switch doesn't contain default section and no case label matches the value, no code is executed and control is tranferred outside the switch statement.
C# Switch |
---|
int i = 1;
switch (i)
{
case 1:
Console.WriteLine("One");
break;
case 2:
Console.WriteLine("Two");
break;
}
|
Output |
---|
One
|
C# Switch |
---|
int i = 2;
switch (i)
{
case 1:
Console.WriteLine("One");
break;
case 2:
Console.WriteLine("Two");
break;
}
|
Output |
---|
Two
|
C# Switch |
---|
int i = 3;
switch (i)
{
case 1:
Console.WriteLine("One");
break;
case 2:
Console.WriteLine("Two");
break;
}
|
Output |
---|
|
Switch with Multiple Case Labels
Before each switch section can be more than one case labels. Such switch section is executed if any of the case labels matches the value.
C# Switch |
---|
int i = 1;
switch (i)
{
case 1:
case 2:
Console.WriteLine("One or Two");
break;
default:
Console.WriteLine("Other");
break;
}
|
Output |
---|
One or Two
|
C# Switch |
---|
int i = 2;
switch (i)
{
case 1:
case 2:
Console.WriteLine("One or Two");
break;
default:
Console.WriteLine("Other");
break;
}
|
Output |
---|
One or Two
|
C# Switch |
---|
int i = 3;
switch (i)
{
case 1:
case 2:
Console.WriteLine("One or Two");
break;
default:
Console.WriteLine("Other");
break;
}
|
Output |
---|
Other
|
Switch with Enum
Switch can be also used with enum values. Mostly it's good practice to include also default section and throw an exception for unexpected values.
Enum |
---|
public enum State { Active = 1, Inactive = 2 }
|
C# Switch |
---|
State state = State.Active;
switch (state)
{
case State.Active:
Console.WriteLine("Active");
break;
case State.Inactive:
Console.WriteLine("Inactive");
break;
default:
throw new Exception(String.Format("Unknown state: {0}", state));
}
|
Output |
---|
Active
|
Enum |
---|
public enum State { Active = 1, Inactive = 2 }
|
C# Switch |
---|
State state = State.Inactive;
switch (state)
{
case State.Active:
Console.WriteLine("Active");
break;
case State.Inactive:
Console.WriteLine("Inactive");
break;
default:
throw new Exception(String.Format("Unknown state: {0}", state));
}
|
Output |
---|
Inactive
|
Enum |
---|
public enum State { Active = 1, Inactive = 2 }
|
C# Switch |
---|
State state = (State)3; // maybe new value in future
switch (state)
{
case State.Active:
Console.WriteLine("Active");
break;
case State.Inactive:
Console.WriteLine("Inactive");
break;
default:
throw new Exception(String.Format("Unknown state: {0}", state));
}
|
Output |
---|
System.Exception: Unknown state: 3
|
Switch with String
Switch can also use string literals to determine which switch section should be executed.C# Switch |
---|
string commandName = "start";
switch (commandName)
{
case "start":
Console.WriteLine("Starting service...");
StartService();
break;
case "stop":
Console.WriteLine("Stopping service...");
StopService();
break;
default:
Console.WriteLine(String.Format("Unknown command: {0}", commandName));
break;
}
|
Output |
---|
Starting service...
|
C# Switch |
---|
string commandName = "stop";
switch (commandName)
{
case "start":
Console.WriteLine("Starting service...");
StartService();
break;
case "stop":
Console.WriteLine("Stopping service...");
StopService();
break;
default:
Console.WriteLine(String.Format("Unknown command: {0}", commandName));
break;
}
|
Output |
---|
Stopping service...
|
C# Switch |
---|
string commandName = "pause";
switch (commandName)
{
case "start":
Console.WriteLine("Starting service...");
StartService();
break;
case "stop":
Console.WriteLine("Stopping service...");
StopService();
break;
default:
Console.WriteLine(String.Format("Unknown command: {0}", commandName));
break;
}
|
Output |
---|
Unknown command: pause
|
See also
- MSDN switch - switch keyword in C# Reference
- C# Foreach - how foreach and IEnumerable works debuggable online
- C# Switch - switch statement examples debuggable online
- C# Using - using statement examples debuggable online
Tips
- [C#] String Format for DateTime – format date and time popular
- [C#] String Format for Double – format float numbers popular