All posts

Show me your IsNumeric() and I'll show you mine

I was innocently writing a piece of code today and the need arose to check if an input type is a number. Naturally I started looking for such a method inside the .NET classes. After all for a framework which has more than 380,000 methods it will only be logical to have at least one which will do something as basic as this. Guess what? .NET does not ship with such a public method. Whaaaa?

But do stay with me, please! It gets better. Armed with Reflector I decided that even if there is no such public method there must be a private one at least! Thank God there is one. Here is what I found inside the System.Linq.Expressions.Expression:

private static bool IsNumeric(Type type)
{
   
type = GetNonNullableType(type);
   
if (!type.IsEnum)
   
{
       
switch (Type.GetTypeCode(type))
       
{
           
case TypeCode.Char:
           
case TypeCode.SByte:
           
case TypeCode.Byte:
           
case TypeCode.Int16:
           
case TypeCode.UInt16:
           
case TypeCode.Int32:
           
case TypeCode.UInt32:
           
case TypeCode.Int64:
           
case TypeCode.UInt64:
           
case TypeCode.Single:
           
case TypeCode.Double:
               
return true;
       
}
   
}
   
return false;
}

At first glance this code appears totally normal, but if you are more careful you will notice that one TypeCode is missing. Can you guess which?

(scroll down for the answer)

 

 

 

 

 

 

 

 

Answer: TypeCode.Decimal

Why the decimal is not a numeric data type according to Microsoft is completely beyond me. Perhaps some of you guys know?

Facebook Twitter DZone It! Digg It! StumbleUpon Technorati Del.icio.us NewsVine Reddit Blinklist Add diigo bookmark

Comments  5

  • 02 Sep

    Maybe there would be another method in .net 4.0: IsNumericEx(Type) which would return IsNumeric(type) || Type.GetTypeCode(type) == TypeCode.Decimal

    Mike

  • 02 Sep

    Actually there is yet another IsNumeric implementation in the .Net 3.0 System.Workflow.Activities.dll that contains the infamous Decimal case -- System.Workflow.Activities.Rules.CastExpression.IsNumeric(...). Why there are two implementations of the same method and they differ from one another is beyond my comprehension ;).

    Manol Donev

  • 02 Sep

    I found two more: - Microsoft.VisualBasic.CompilerServices.Versioned.IsNumeric - Microsoft.VisualBasic.Information.IsNumeric

    Atanas Korchev

  • 02 Sep

    JavaScript is better! You need to use !isNaN(value) :)

    Vlad

  • 02 Sep

    Thanks for the comments guys! Actually the ones in the Microsoft.Visual.Basic assembly are public API, although a bit bizzare. The one in Microsoft.VisualBasic.Information.IsNumeric is checking for StackOverflowException, OutOfMemoryException and ThreadAbortException! I doubt I'm gonna need that :) The one in the CastExpression class does not seem to support nullable types and is not public. So unfortunately it is not usable as well. The javascript code, however, is simply brilliant :) Thanks, Vlad! Too bad we cannot use it :) At the end of the day we just modified the IsNumeric method from this post to support Decimals.

    Vladimir Milev

Post a comment
  1. Formatting options
       
     
     
     
     
       
  2. Security image