How to check if a number is an integer in .NET?
问题 Say I've got a string which contains a number. I want to check if this number is an integer. Examples IsInteger("sss") => false IsInteger("123") => true IsInterger("123.45") =>false 回答1: You can use int.TryParse. It will return a bool if it can parse the string and set your out parameter to the value int val; if(int.TryParse(inputString, out val)) { //dosomething } 回答2: There are two immediate options that you can use. Option 1 - preferred - use Int32.TryParse. int res; Console.WriteLine(int