Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Any performance difference between int.Parse() and Convert.Toint()?

Tags:

performance

c#

Is there any significant advantages for converting a string to an integer value between int.Parse() and Convert.ToInt32() ?

string stringInt = "01234";  int iParse = int.Parse(stringInt);  int iConvert = Convert.ToInt32(stringInt); 

I found a question asking about casting vs Convert but I think this is different, right?

like image 823
CmdrTallen Avatar asked Mar 13 '09 02:03

CmdrTallen


People also ask

Which is better int Parse or convert ToInt32?

Convert. ToInt32 allows null value, it doesn't throw any errors Int. parse does not allow null value, and it throws an ArgumentNullException error.

What is the difference between Parse and convert in C#?

Parse and Convert ToInt32 are two methods to convert a string to an integer. The main difference between int Parse and Convert ToInt32 in C# is that passing a null value to int Parse will throw an ArgumentNullException while passing a null value to Convert ToInt32 will give zero.

What is the difference between convert ToInt32 and convert toint64?

Thus int32 would be limited to a value between (-256^4/2) and (256^4/2-1). And int64 would be limited to a value between (-256^8/2) and (256^8/2-1).

Which statement accurately describes the difference between Int32 TryParse () and convert ToInt32 ()?

Parse() and Int32. TryParse() can only convert strings. Convert. ToInt32() can take any class that implements IConvertible .


1 Answers

When passed a string as a parameter, Convert.ToInt32 calls int.Parse internally. So the only difference is an additional null check.

Here's the code from .NET Reflector

public static int ToInt32(string value) {     if (value == null)     {         return 0;     }     return int.Parse(value, CultureInfo.CurrentCulture); } 
like image 122
Rob Windsor Avatar answered Oct 11 '22 18:10

Rob Windsor