We have already seen in C# Switch - Case, the example of you being a teacher.
Now, we will look at the same example,
Roll Number | Name |
---|---|
1 | Ronald |
2 | John |
3 | Murali |
4 | Satish |
5 | Debasish |
public class MyApplication { public static void Main(string[] args) { string name = "John"; switch (name) { case "Ronald": System.Console.WriteLine("His roll number is 1"); break; case "John": System.Console.WriteLine("His roll number is 2"); break; case "Murali": System.Console.WriteLine("His roll number is 3"); break; case "Satish": System.Console.WriteLine("His roll number is 4"); break; case "Debasish": System.Console.WriteLine("His roll number is 5"); break; default: System.Console.WriteLine("The student does not exist."); break; } } }
This time we are trying to match a String in the Switch - Case statement.
We have taken the name in a String variable name and initialised it with "John".
string name = "John";
Then we have taken the name and put it in switch.
switch (name) { case "Ronald": System.Console.WriteLine("His roll number is 1"); break; case "John": System.Console.WriteLine("His roll number is 2"); break; case "Murali": System.Console.WriteLine("His roll number is 3"); break; case "Satish": System.Console.WriteLine("His roll number is 4"); break; case "Debasish": System.Console.WriteLine("His roll number is 5"); break; default: System.Console.WriteLine("The student does not exist."); break; }
And checked which case matches "John".
And found
case "John": System.Console.WriteLine("His roll number is 2"); break;
So printed John's roll number
His roll number is 2