Complete the code to match the integer 5 in the switch expression.
int number = 5; switch (number) { case [1]: Console.WriteLine("Number is five"); break; default: Console.WriteLine("Number is not five"); break; }
The case label must be the integer 5 to match the value of number.
Complete the code to use pattern matching to check if the object is a string.
object obj = "hello"; switch (obj) { case [1] s: Console.WriteLine($"String with length {s.Length}"); break; default: Console.WriteLine("Not a string"); break; }
The pattern string s matches if obj is a string and assigns it to s.
Fix the error in the switch expression to correctly match a nullable int with value 10.
int? number = 10; switch (number) { case [1]: Console.WriteLine("Number is ten"); break; default: Console.WriteLine("Number is not ten"); break; }
int? n which matches nullable but cannot use when condition properly.The pattern int n when n == 10 matches a non-null int with value 10 inside the nullable.
Fill both blanks to create a switch expression that returns a description for the shape object.
object shape = new Circle();
string description = shape switch
{
[1] => "It's a circle",
[2] => "It's a square",
_ => "Unknown shape"
};The patterns Circle c and Square s match objects of those types.
Fill all three blanks to create a switch expression that returns a message based on the input object type and value.
object input = 42; string message = input switch { [1] when [2] > 40 => "Large integer", [3] => "Not a large integer", _ => "Unknown" };
The first pattern matches an int and uses n in the condition. The second pattern matches a string.