■
Note
C# demands that each case (including
default
) that contains executable statements have a terminating
break
or
goto
to avoid fall-through.
One nice feature of the C# switch statement is that you can evaluate string data in addition to
numeric data. Here is an updated switch statement that does this very thing (notice we have no
need to parse the user data into a numeric value with this approach):
static void ExecuteSwitchOnString()
{
Console.WriteLine("C# or VB");
Console.Write("Please pick your language preference: ");
string langChoice = Console.ReadLine();
switch (langChoice)
{
case "C#":
Console.WriteLine("Good choice, C# is a fine language.");
break;
case "VB":
Console.WriteLine("VB .NET: OOP, multithreading and more!");
break;
default:
Console.WriteLine("Well...good luck with that!");
break;
}
}
Do'stlaringiz bilan baham: |