C - how to determine whether a Type is a number
Interview Questions practice on Codemia
Over 8,000 real interview questions from top companies, searchable by company and role.
Introduction
In C#, determining whether a Type is numeric is trickier than it first looks because there is no single built-in IsNumeric property on System.Type. The practical solution depends on context. For runtime reflection, a TypeCode-based check or an explicit whitelist of numeric types is usually the clearest answer.
Why IsPrimitive Is Not Enough
A first attempt is often:
That is not sufficient because bool and char are primitive too, and decimal is numeric but not primitive.
So this is wrong for numeric detection:
It answers a different question.
Use TypeCode
One common runtime approach is to switch on Type.GetTypeCode.
This is a good general-purpose runtime helper because it is explicit and handles nullable numeric types too.
Why Nullable Support Matters
If you do reflection over models or DTOs, you will often encounter int?, decimal?, and similar nullable wrappers.
That is why this line matters:
Without it, typeof(int?) would not match the numeric cases even though it clearly represents a nullable numeric value.
Explicit Type Whitelist
Another clear approach is to compare against exact known numeric types.
This is slightly more verbose than TypeCode, but some teams prefer the directness because the supported set is visible immediately.
What About char, bool, and Enums
Even though some of these can participate in conversions or have underlying integer representations, they are usually not treated as numeric types for application logic.
Typical expectations:
- '
charis not numeric' - '
boolis not numeric' - '
enumis not numeric for most validation purposes'
If your business rules differ, define that explicitly rather than assuming the runtime has the same definition you do.
Compile-Time Generic Constraints
If you are designing new generic numeric code in modern .NET, runtime reflection may not be the best tool. Newer generic math interfaces can express numeric requirements at compile time.
For example:
This does not answer “is this Type numeric?” at runtime, but it is often the better design if the real goal is to write generic numeric code safely.
Reflection Use Cases
Runtime numeric detection often shows up in:
- object mappers
- validation frameworks
- serialization code
- UI scaffolding or metadata inspection
In those scenarios, a TypeCode helper is usually enough and easier to maintain than complex dynamic logic.
Common Pitfalls
The biggest mistake is using IsPrimitive and assuming it means “numeric.” Another is forgetting nullable wrappers and therefore rejecting types like decimal?. Developers also sometimes include char or enums accidentally because they are backed by numeric representations, even though that is not the intended business meaning. Finally, if the actual problem is generic numeric computation, a reflection helper may be the wrong abstraction compared with modern generic math constraints.
Summary
- There is no built-in
Type.IsNumericin C#. - For runtime checks,
TypeCodeor an explicit whitelist is the clearest solution. - Unwrap nullable types before testing.
- Do not confuse numeric types with primitive types.
- For new generic numeric algorithms, consider compile-time numeric interfaces instead of runtime reflection.
Related reading
- C - Making a Process.Start wait until the process has start-up
- C 3.0 auto-properties — useful or not?
- C 5 async CTP why is internal state set to 0 in generated code before EndAwait call?
- C '' before a String
- C Adding context to Parallel.ForEach in ASP.NET
- C Adding working days from a cetain date
- C algorithm for generating hierarchy
- C and arrays of anonymous objects

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.