Skip to main content
C# / .NET Output Prediction Hard +35 XP

Pattern Matching Switch

What does this print when shape is a Circle?

C# / .NETRead-only
string Describe(object shape) => shape switch {
  Circle c => $"Circle r={c.Radius}",
  Square s => $"Square side={s.Side}",
  _ => "Unknown"
};

What is the output?

Show answer and explanation

Answer: Circle r=<radius>

C# switch expressions with pattern matching check the runtime type. When shape is a Circle, the first arm matches.

Hint: The shape type determines which arm matches.

What this challenge tests

Output Prediction in C# / .NET: pattern-matching, switch-expressions.

Related challenges