How to Check if string is valid Number in C#?

Updated on     Kisan Patel

Use the static TryParse method of any of the numeric types.

For example, to determine whether a string contains a double, use the following method:

string str = "10.5";
double result = 0;
if (double.TryParse(str,
     System.Globalization.NumberStyles.Float,
     System.Globalization.NumberFormatInfo.CurrentInfo,
     out result))
{
    // Is a double!
}

Here is another Example:

int n;
bool isNumeric = int.TryParse("123", out n);

C#

Leave a Reply