c# check if string is numeric only code example
Example 1: c# how to check string is number
string s1 = "123";
string s2 = "abc";
bool isNumber = int.TryParse(s1, out int n);
isNumber = int.TryParse(s2, out int n);
Example 2: c# how do you check if a string contains only digits
if (str.All(char.IsDigit)) {
Console.WriteLine("This string only contains numbers");
}
Example 3: c# check if string is all numbers
if (str.All(char.IsDigit)) {
}
Example 4: c# see if string is int
bool result = int.TryParse("123", out var n);
Example 5: verify if number c#
var isNumeric = int.TryParse("123", out int n);