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.
9 Answers
public bool IsNumeric(string value)
{ return value.All(char.IsNumber);
} 9 To totally steal from Bill answer you can make an extension method and use some syntactic sugar to help you out.
Create a class file, StringExtensions.cs
Content:
public static class StringExt
{ public static bool IsNumeric(this string text) { double test; return double.TryParse(text, out test); }
}EDIT: This is for updated C# 7 syntax. Declaring out parameter in-line.
public static class StringExt
{ public static bool IsNumeric(this string text) => double.TryParse(text, out _);
}Call method like such:
var text = "I am not a number";
text.IsNumeric() //<--- returns false 5 You could make a helper method. Something like:
public bool IsNumeric(string input) { int test; return int.TryParse(input, out test);
} 3 It is worth mentioning that one can check the characters in the string against Unicode categories - numbers, uppercase, lowercase, currencies and more. Here are two examples checking for numbers in a string using Linq:
var containsNumbers = s.Any(Char.IsNumber);
var isNumber = s.All(Char.IsNumber);For clarity, the syntax above is a shorter version of:
var containsNumbers = s.Any(c=>Char.IsNumber(c));
var isNumber = s.All(c=>Char.IsNumber(c));Link to unicode categories on MSDN:
menu: Project-->Add Reference
click: assemblies, framework
Put a checkmark on Microsoft.VisualBasic.
Hit OK.
That link is for Visual Studio 2013, you can use the "Other versions" dropdown for different versions of visual studio.
In all cases you need to add a reference to the .NET assembly "Microsoft.VisualBasic".
At the top of your c# file you neeed:
using Microsoft.VisualBasic;Then you can look at writing the code.
The code would be something like:
private void btnOK_Click(object sender, EventArgs e) { if ( Information.IsNumeric(startingbudget) ) { MessageBox.Show("This is a number."); } } 11 Using C# 7 (.NET Framework 4.6.2) you can write an IsNumeric function as a one-liner:
public bool IsNumeric(string val) => int.TryParse(val, out int result);Note that the function above will only work for integers (Int32). But you can implement corresponding functions for other numeric data types, like long, double, etc.
Try following code snippet.
double myVal = 0;
String myVar = "Not Numeric Type";
if (Double.TryParse(myVar , out myNum)) { // it is a number
} else { // it is not a number
} I usually handle things like this with an extension method. Here is one way implemented in a console app:
namespace ConsoleApplication10
{ class Program { static void Main(string[] args) { CheckIfNumeric("A"); CheckIfNumeric("22"); CheckIfNumeric("Potato"); CheckIfNumeric("Q"); CheckIfNumeric("A&^*^"); Console.ReadLine(); } private static void CheckIfNumeric(string input) { if (input.IsNumeric()) { Console.WriteLine(input + " is numeric."); } else { Console.WriteLine(input + " is NOT numeric."); } } } public static class StringExtensions { public static bool IsNumeric(this string input) { return Regex.IsMatch(input, @"^\d+$"); } }
}Output:
A is NOT numeric.
22 is numeric.
Potato is NOT numeric.
Q is NOT numeric.
A&^*^ is NOT numeric.
Note, here are a few other ways to check for numbers using RegEx.
2Is numeric can be achieved via many ways, but i use my way
public bool IsNumeric(string value)
{ bool isNumeric = true; char[] digits = "0123456789".ToCharArray(); char[] letters = value.ToCharArray(); for (int k = 0; k < letters.Length; k++) { for (int i = 0; i < digits.Length; i++) { if (letters[k] != digits[i]) { isNumeric = false; break; } } } return isNumeric;
} 3