IsNumeric .Net extension in C # and VB.NET

Here is a quick and dirty IsNumeric extension method in C#. The method uses Regex to determine if input is a numeric value.

C# version:

public static bool IsNumeric(this object number)
      {
          return number == null ? false : Regex.IsMatch(number.ToString(), @"^[-+]?[0-9]+(\.[0-9]{1,2})?\Z");
      }

VB.NET version:

 <System.Runtime.CompilerServices.Extension> _
   Public Function IsNumeric(number As Object) As Boolean
       Return If(number Is Nothing, False, Regex.IsMatch(number.ToString(), "^[-+]?[0-9]+(\.[0-9]{1,2})?\Z"))
 
   End Function

The pattern will match -##.##, +##.## ,##.## and #.#

Comments (2) -

Why did you make it and object extension?
Would't be easier to use a string?

Eloy,
All objects in .Net inherit from base object.
string is an immutable object and I wanted to avoid it.

Add comment