There are two simple methods,
1. Don't use tryparse, so you can write one yourself:
Using system; Class test {static void main () {string S1 = console. readline (); int I; If (mytryparse (S1, out I) {console. writeline ("succeeded:" + I);} else {console. writeline ("failed: enter a number! ") ;}} Static bool mytryparse (string S, out int I) {try {int temp = convert. toint32 (s);} catch {I =-1; return false;} If (convert. toint32 (s)> = 0 | convert. toint32 (s) <0) {I = convert. toint32 (s); Return true ;}else {I =-1; return false ;}}}
Second: Use the method provided by laoliu, a netizen, and draw on the advantages of JavaScript parseint () to enrich laoliu'sCode:
Using system; Class test {static void main () {console. writeline (s2i (console. readline ();} static int s2i (string s) {char [] C1 = S. tochararray (); int result = 0; For (INT I = 0; I <c1.length; I ++) {If (c1 [I]> = 48 & c1 [I] <= 57) Result = (result * 10) + c1 [I]-'0'; elsecontinue ;} return result ;}}