csharp check if string is all numbers

Code Example - csharp check if string is all numbers

                
                        if (str.All(char.IsDigit)) {
  // String only contains numbers
}
                    
                
 

csharp how to check string is number

                        
                                string s1 = "123";
string s2 = "abc";

bool isNumber = int.TryParse(s1, out int n); // returns true
isNumber = int.TryParse(s2, out int n); // returns false
                            
                        
 

csharp how do you check if a string contains only digits

                        
                                if (str.All(char.IsDigit)) {
  Console.WriteLine("This string only contains numbers");
}