+ 1

what's the difference betweet int.TryParse and ConvertTo ?

30th Jun 2019, 3:57 PM
Fsg So
2 Answers
+ 2
1) int.TryParse returns true or false byte x = 5; Console.WriteLine(int.TryParse(x)); Output: True 2) ConvertTo concerts (or throws exception) double x = 25.54; int y = Convert.ToInt32(x); Console.WriteLine(y); Output: 25 Read more about it here https://www.cambiaresearch.com/articles/68/convert-string-to-int-in-csharp
30th Jun 2019, 4:05 PM
HNNX 🐿
HNNX 🐿 - avatar
0
You can use int.TryParse to check if the variable is an integer or not, so if it is an integer it returns true and also the converted integer result. Convert.ToInt32 is for converting the variable and if the variable is not an integer it throws exception. When you are using convert method to check the variable’s type, you have to put it in try-catch blocks.
2nd Jul 2019, 12:36 AM
Yaya
Yaya - avatar