C# char.IsDigit (If Char Is Between 0 and 9)Invoke the char.IsDigit static method. A digit char is between 0 and 9.
This method checks character values. It determines if a character in the string is a digit character—meaning it is part of an Arabic number—in the range 0-9. It is useful in parsing, scanning or sorting algorithms.Char
the char.IsDigit method is a static method on the System.Char struct, which is aliased to the char keyword in the C# programming language. It returns a Boolean value, which tells you whether the character is a digit character.Bool MethodStatic
Also: It contains some logic for globalization, which checks for Latin characters.
Info: The program defines the Main entry point and an input string. The string contains lowercase letters, punctuation, and digits.
And: In the output, IsDigit returns True only for the digits. It accurately detects digit chars.True, False
C# program that tests for digits
static void Main()
string test = "Abc,123";
foreach (char value in test)
bool digit = char.IsDigit(value);
/// Returns whether the char is a digit char.
/// Taken from inside the char.IsDigit method.
public static bool IsCharDigit(char c)
return ((c >= '0') && (c <= '9'));
As Raymond Chen at Microsoft notes, the IsDigit method has some interesting behavior on non-Latin numbers, meaning certain rare Unicode characters. This is mainly a security concern when using it in input validation.
Therefore: Using a custom IsCharDigit method is safer for simple tasks. The logic in IsCharDigit is taken directly from the main part of char.IsDigit.
The char.IsDigit method works in normal situations with digits between 0 and 9. We used it in a foreach-loop. It has an unusual behavior that complicates its behavior. This could lead to unexpected behavior.
Tip: You can extract the character-testing logic for more predictable behavior and slightly better performance.
© 2007-2019 Sam Allen. Every person is special and unique. Send bug reports to email@example.com.