+ 1
what's the difference betweet int.TryParse and ConvertTo ?
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
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.