Skip to content
Sharp

Language Upgrades · interview question

In a pattern matching switch expression involving a sealed class hierarchy, how can you ensure that all possible subclasses are handled?

Multiple choiceHardchapter-12-curatedpremiumlanguage-upgradespattern-matching
Choose one answer
Answer and explanation

Correct answer

C. Include a case for each subclass explicitly and avoid a default case.

Why

To ensure all possible subclasses in a sealed class hierarchy are handled, you should include a case for each subclass explicitly in the switch expression. This approach leverages exhaustiveness checking, which avoids the need for a default case.

Test yourself on Language Upgrades

A free test with instant, explained results. No account needed.

Start free test
All Language Upgrades questions →