0
Convert string to int on c#
Which way is the best to convert an string to an int on c#? Iâve seen the â.Toint32â, the âtry method with an ifâ and others⊠I wanna know which way is the most common used so that I can put on my code. // Iâm a beginner :)
1 Answer
+ 2
"best" perhaps isn't what you want eventually, not even sure there was one anyways. "most common" may also depend on some factors like the coder's or their team preference.
If it was safety that you seek, you might consider the TryParse() method which does not throw an exception upon conversion problems, such as presence of non numeric characters/symbols in the source string to be converted, which may prevent a smooth conversion process.
https://learn.microsoft.com/en-us/dotnet/csharp/programming-guide/types/how-to-convert-a-string-to-a-number