-1

Where we need to use Convert.Int() and Int.Parse(). So what is the difference between Convert.Int(), Int.Parse() and Int.TryParse.I know that Int.TryParse returns us true/false and perfomance of Int.TryParse better than other methods.

1.Why we need Convert.Int() and Int.Parse(). 2.Actually we can use Int.TryParse in everywhere .

2 Answers2

1

It is Convert.ToInt32

Int.Parse() It's used to convert the input into integer. The input integer should be a string which contains only numbers. it throw error if string is null.

Convert.ToInt32

It's used to convert the input into integer and bool. The input integer should be an integer; if it's null it will return 0; if it's string, it should only contain the number. It's handling null value and returning 0.

converting the String (which contains long value) to integer.

Convert.ToInt32 and Int.Prase throw exception but Int.TryParse don't throw exception

Uses

Convert.ToInt32 :- when need to null automatically

Int.TryParse:- when string contain long number and use if condition

Otherwise use int.Pase

Click Here For More Details

Pradeep Kumar
  • 1,193
  • 1
  • 9
  • 21
0

That could be true, but ConvertTo.Int could be an old version left there same as int.Parse but int.TryPasre is just synthetic sugar which means it is better in style and error handling

Oleg_B
  • 34
  • 1
  • 1
  • 7