C# Equivalent of VB IsNumeric() function

Here is an extension method designed to make the VB IsNumeric() function available to C#.

Simply place the below extension method function into your extension method class, or copy the below into a new file if you don’t already have one; Extension methods are great, so go start making some!

/// <summary>
/// Extension Methods used Globally
/// </summary>
public static class ExtensionMethods
  public static bool IsNumeric(this string s)
    double i = 0;
    if (Double.TryParse(s, out i))
      // Numeric
      return true;
      // Non-Numeric
      return false;

This will make the IsNumeric() function available to any string, so call by using myString.IsNumeric()

string myString = "Hello";
if (myString.IsNumeric())
  return "I'm a number";
  return "I'm some characters";

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>