How to implement Visual Basic .NET IsNumeric functionality by using Visual C#

Article translations Article translations
Article ID: 329488 - View products that this article applies to.
This article was previously published under Q329488
Expand all | Collapse all

On This Page


This step-by-step article describes how to use the Microsoft Visual Basic .NET IsNumeric function in Visual C#. The IsNumeric function returns a Boolean value that indicates whether an expression can be evaluated as a number. The IsNumeric function returns True when the data type of the expression is Short, Integer, Long, Decimal, Single, or Double. It also returns True if the expression is a string that can be successfully converted to a Double.

Create the IsNumeric Function

In Visual C#, you can use the Double.TryParse method to obtain functionality that is similar to IsNumeric. Double.TryParse converts the string representation of a number in a specified style and culture-specific format to its double-precision floating point number equivalent. To create the IsNumeric function:
  1. Start Visual Studio. On the File, point to New, and then click Project.
  2. In the New Project dialog box, click Visual C# under Project Type.

    Note In Visual Studio 2003, click Visual C# Projects under Project Types.
  3. Under Templates, click Console Application, and then click OK. By default, Program.cs is created.
  4. At the end of the Program class, add the following code for the IsNumeric function:
    // IsNumeric Function
    static bool IsNumeric(object Expression)
    // Variable to collect the Return value of the TryParse method.
    	bool isNum;
    // Define variable to collect out parameter of the TryParse method. If the conversion fails, the out parameter is zero.
    	double retNum;
    // The TryParse method converts a string in a specified style and culture-specific format to its double-precision floating point number equivalent.
    // The TryParse method does not generate an exception if the conversion fails. If the conversion passes, True is returned. If it does not, False is returned.
    	isNum = Double.TryParse(Convert.ToString(Expression), System.Globalization.NumberStyles.Any, System.Globalization.NumberFormatInfo.InvariantInfo, out retNum );
    	return isNum;

Test the Code

To test the IsNumeric function:
  1. Add the following code to the Main method of Program:
    // Test a numeric string. 
    Console.WriteLine("\"123\" is numeric : {0}", IsNumeric("123"));
    // Test a number.
    Console.WriteLine("-5.64 is numeric : {0}", IsNumeric(-5.64));
    // Test infinity.
    Console.WriteLine("-Infinity is numeric : {0}", IsNumeric("-Infinity"));
    // Test a general string.
    Console.WriteLine("\"12ABC\" is numeric : {0}", IsNumeric("12ABC"));
    // Test with null.
    Console.WriteLine("null is numeric : {0}", IsNumeric(null));
  2. On the Debug menu, click Start to run the code. Verify the results in the console window.
  3. Press ENTER to close the console window.


For more information about the TryParse function, visit the following Microsoft Developer Network (MSDN) Web site:
Double.TryParse Method
For additional information, click the following article number to view the article in the Microsoft Knowledge Base:
325961 HOW TO: Call Visual Basic .NET Run-Time Library Members from Visual C# .NET


Article ID: 329488 - Last Review: July 16, 2008 - Revision: 3.1
  • Microsoft Visual C# .NET 2002 Standard Edition
  • Microsoft Visual C# 2005 Express Edition
  • Microsoft Visual C# 2008 Express Edition
kbstring kbconsole kbhowtomaster KB329488

Give Feedback


Contact us for more help

Contact us for more help
Connect with Answer Desk for expert help.
Get more support from