Switch case on type c
Interview Questions practice on Codemia
Over 8,000 real interview questions from top companies, searchable by company and role.
Introduction
In C#, switching on runtime type is a normal pattern, but the best syntax depends on the language version. Modern C# uses type patterns in switch expressions or switch statements, while older code often fell back to if with is checks or manual type comparisons.
Modern C# Type Pattern Matching
A clean current approach is a switch statement with type patterns.
This is usually what people mean today by “switch on type.”
switch Expression Version
If the logic is purely value-to-value mapping, a switch expression can be even clearer.
This is concise and works well when each branch returns a single result.
Older Alternatives
Before type patterns were available, developers often used if chains.
That still works, but modern pattern matching is usually easier to read and less repetitive.
Exact Type Versus Assignable Type
There is an important semantic difference between checking the runtime type exactly and checking whether an object is assignable to a base type or interface.
Pattern matching with case BaseType x: matches derived types too. If you need exact type equality, compare value.GetType() to a specific type instead.
That distinction matters in inheritance-heavy code.
A Practical Example With Interfaces
This shows why type-based switching is useful: it can express dispatch logic cleanly when the runtime shape matters.
When Not to Switch on Type
A type switch is sometimes a code smell. If the code keeps branching on concrete types everywhere, you may really want polymorphism instead.
For example, if each type knows how to describe or process itself, a virtual method or interface method may be cleaner than repeated external type checks.
So the right question is not only “can I switch on type,” but also “should this behavior live inside the type instead.”
Common Pitfalls
Pattern Matching Can Add Conditions
Modern C# patterns can also include guards, which makes them useful for type-plus-condition logic instead of plain type dispatch only.
The most common mistake is forgetting that a base-type case can also match derived instances, which changes branch selection.
Another mistake is using type switches where polymorphism would express the design more cleanly.
Developers also often omit a null case and then debug a surprising NullReferenceException later.
Summary
- Modern C# supports switching on type through pattern matching.
- '
switchstatements andswitchexpressions both work well for this.' - Type-pattern matches are based on assignability, not always exact type equality.
- Include a
nullcase when the input may be null. - If type switching spreads everywhere, consider whether polymorphism would be better.
Related reading
- Synchronous I/O within an async/await-based Windows Service
- System.BadImageFormatException Could not load file or assembly from installutil.exe
- System.Diagnostics.Debug.WriteLine in production code
- System.MissingMethodException Method not found?
- ''System.Net.Http.HttpContent'' does not contain a definition for ''ReadAsAsync'' and no extension method
- System.Net.WebException The remote name could not be resolved
- System.ObjectDisposedException handle is destroyed
- System.OutOfMemoryException when generating permutations

OOD Fundamentals
Master object-oriented design from first principles, SOLID, design patterns, and classic interview problems with hands-on coding.
View the courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
Interview Questions practice on Codemia
Over 8,000 real interview questions from top companies, searchable by company and role.