Converting string to int (or something else), is there a prefered way?

c#, type-conversion

Solution

The Convert.ToInt32 is actually implemented the following way...

int.Parse(value, CultureInfo.CurrentCulture);

...which is the same as your stated alternative except it takes into account the culture settings. The int.Parse method is itself implemented the following way...

Number.ParseInt32(s, NumberStyles.Integer, NumberFormatInfo.GetInstance(provider));

...where Number is an internal class that you cannot call directly. The Number.ParseInt32 method is marked with the following attribute...

[MethodImpl(MethodImplOptions.InternalCall)]

...showing that it is implemented inside the CLR itself.

Problem

Sometimes i wonder, when converting strings to other types, for example int32, is one way better than the other, or is it just a matter of taste? ``` Convert.ToInt32("123") ``` or ``` Int32.Parse("123") ``` or Some other way? Not considering the case if it's not a valid int in this question.

Original source

Related problems