Practice mode C#

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.

Report a problem

Answer checked How we write and check questions

Tip: press A, B, C or D to answer, and N for the next question.

Advertisement

Have a question about this answer?

Your email address will not be published. Required fields are marked *

Links and website addresses are not allowed in comments.