Double.Parse() vs Convert.ToDouble()

I don't know why I like to do these little tests, but I like doing little things as efficient as I can.

Lutz Roeder made a .NET reflector that will dissasemble .NET Liberaries. Here is the data that was taken back when I used this on Double.Parse and Convert.ToDouble()

Double.Parse("")

public static double Parse(string s)

{

      return double.Parse(s, NumberStyles.Float | NumberStyles.AllowThousands, NumberFormatInfo.CurrentInfo);

}


Will Call:

private static double Parse(string s, NumberStyles style, NumberFormatInfo info)

{

      double num1;

      try

      {

            num1 = Number.ParseDouble(s, style, info);

      }

      catch (FormatException)

      {

            string text1 = s.Trim();

            if (text1.Equals(info.PositiveInfinitySymbol))

            {

                  return double.PositiveInfinity;

            }

            if (text1.Equals(info.NegativeInfinitySymbol))

            {

                  return double.NegativeInfinity;

            }

            if (!text1.Equals(info.NaNSymbol))

            {

                  throw;

            }

            return double.NaN;

      }

      return num1;

}


Convert.ToDouble()


public static double ToDouble(string value)

{

      if (value == null)

      {

            return 0;

      }

      return double.Parse(value, CultureInfo.CurrentCulture);

}



So Convert.ToDouble actually calls Double.Parse.(""). SO the long story short, Convert.ToDouble is actually a bit easier than doing:



Double.Parse(MyVariable.ToString());




No Comments