switch expression bools

Solutions on MaxInterview for switch expression bools by the best coders in the world

showing results for - "switch expression bools"
Sofia
13 Oct 2018
1string startsWith = "somestring:";
2
3// Note: this works with any data type
4 switch (startsWith)
5 {
6	 // Using the 'when' keyword you can convert your case to a bool like
7     // expression like so:
8     case string when startsWith.StartsWith("somestring:"):
9         Console.WriteLine("hit");
10         break;
11
12   case string when startsWith.StartsWith("someotherstring:"):
13		Console.WriteLine("hit 1");
14		break; 
15}
16
17// Output: hit