What is the value of r after: int n = 3; string r = n switch { 1 => “one”, 3 => “three”, _ => “other” };?
Correct answer A. three
Explanation
A switch expression evaluates its arms from top to bottom and returns the result of the first pattern that matches. n is 3, so the second arm matches and r is "three". The discard pattern _ matches anything and acts as the default. If no arm matched and there were no discard, a SwitchExpressionException would be thrown at run time.
Answer checked How we write and check questions
Discussion