IsNumeric function in c#

user3352070 picture user3352070 · Apr 1, 2014 · Viewed 145.3k times · Source

I know it's possible to check whether the value of a text box or variable is numeric using try/catch statements, but IsNumeric is so much simpler. One of my current projects requires recovering values from text boxes. Unfortunately, it is written in C#.

I understand that there's a way to enable the Visual Basic IsNumeric function in Visual C# by adding a reference to Visual Basic, though I don't know the syntax for it. What I need is a clear and concise walkthrough for enabling the IsNumeric function in C#. I don't plan on using any other functions indigenous to Visual Basic.

Answer

CodeDog picture CodeDog · Jun 22, 2016
public bool IsNumeric(string value)
{
    return value.All(char.IsNumber);
}