Skip to content

David Turvey's Blog

All About Development

Archive

Tag: string

When you want to convert a string to an integer the two most popular approaches are to use int.Parse() or System.Convert.ToInt32(), both of which are static functions. (I’m not going to include int.TryParse(), System.Convert.ChangeType(), etc. in this discussion). Personally I always use int.Parse(), why? Well I don’t actually have a good reason; I’ve always just thought it looks cleaner.

So why would you choose to use one over the other? Well I guess it really depends on what you need; from my short investigation I was able to spot the following differences:

  • Passing a null string into int.Parse() will throw an ArgumentNullException. Passing a null string into System.Convert.ToInt32() will return 0 (i.e. the default for int).
  • int.Parse() has an overload where you can pass in the NumberStyles flags enum to specify how the string should be handled. System.Convert.ToInt32() does not have this overload.
  • System.Convert.ToInt32() has an overload where you can specify the number base of the string, i.e. base 2, 8, 10, or 16.

Available overloads on int:

public static int Parse(string s)
public static int Parse(string s, NumberStyles style)
public static int Parse(string s, IFormatProvider provider)
public static int Parse(string s, NumberStyles style, IFormatProvider provider)

Available overloads on System.Convert (for the input type string):

public static int ToInt32(string value)
public static int ToInt32(string value, IFormatProvider provider)
public static int ToInt32(string value, int fromBase)

Some other points for you:

  • If you don’t provide the IFormatProvider both int.Parse() and Sytem.Convert.ToInt32() will retrieve the format off the CurrentCulture.
  • If you do not specify the NumberStyles for int.Parse() it will simply use NumberStyles.Integer.
  • System.Convert.ToInt32(string) and System.Convert.ToInt32(string, IFormatProvider) both call int.Parse() to do the conversion, where as System.Convert.ToInt32(string, int) calls ParseNumber.StringToInt(). This is a static internal class so you will need to use Red Gate’s .NET Reflector if you want to see it. Something interesting about this call is that it makes an internal call to a method in the CLR to do the conversion.

Looking at the corresponding methods for conversion on the other types it appears they seem to work in a similar way, with the exception that System.Convert.ToDouble() and System.Convert.ToDecimal() do not provide an overload to specify the number base.

I discovered something cool about strings the other day, and I must admit it is incredibly obviously and I do feel a rather stupid for not knowing this. I’ve never seen any code using this technique to enumerate through the characters in a string, maybe you have?

So normally developers would usually do something like this to enumerate through the characters in a string:


char[] charArray = "Hello World".ToCharArray();
foreach (char ch in charArray)
{
    // Do something (with ch)
}

Or maybe:


string value = "Hello World";
for (int index = 0; index < value.Length; index++)
{
    char ch = value[index];
    // Do something (with ch)
}

Or some other technique.

So I wanted to see if string implemented IFormattable, don’t ask why, it was a random thought going through my head and I discovered string implements a number of interesting interfaces, namely:

  • IComparable
  • ICloneable
  • IConvertible
  • IComparable
  • IEnumerable
  • IEnumerable
  • IEquatable

The most interesting being IEnumerable because it allows you to do the following:


foreach (char ch in "Hello World")
{
    // Do something (with ch)
}

And you can do LINQ queries directly against the characters in a string:


var ordinalValues = from ch in "Hello World"
                    select (int)ch;

Which I think is pretty cool :)