Is there a way to determine whether or not a given .Net Type is a number? For example: System.UInt32/UInt16/Double are all numbers. I want to avoid a long switc
None of the solutions takes Nullable into account.
I modified Jon Skeet's solution a bit:
private static HashSet NumericTypes = new HashSet
{
typeof(int),
typeof(uint),
typeof(double),
typeof(decimal),
...
};
internal static bool IsNumericType(Type type)
{
return NumericTypes.Contains(type) ||
NumericTypes.Contains(Nullable.GetUnderlyingType(type));
}
I know I could just add the nullables itself to my HashSet. But this solution avoid the danger of forgetting to add a specific Nullable to your list.
private static HashSet NumericTypes = new HashSet
{
typeof(int),
typeof(int?),
...
};