C# Basics: switch control statement
| Published on 6/8/2009 by Dotnetindex More articles in .NET
|
We will look up Switch statement in C#. Switch statement is another control statement that handles multiple selections and enumerations. It could be replace with IF statement.
int fruit = "Apple";
switch (fruit )
{
case "Orange":
Console.WriteLine("Nothing done");
break;
case "Banana":
Console.WriteLine("Nothing done");
break;
case "Apple":
Console.WriteLine("This must be written on shell");
break;
default:
Console.WriteLine("Nothing done but it\'s the default case");
break;
}
As you've seen example, Switch control looks for the match. If you have not change anything, when you run this script, you must see "This must be written on shell" on console. Change now fruit as "AppleX". The default case will be run and default statement will be seen on shell. |
no comments submitted



