Search code examples
c#.netclrdoubletostring

Inspecting double.ToString()


As a follow up to a question I had about optimising the conversion of double to string for memory (see c# double to character array or alternative) I wanted to see how double.ToString() is implemented under the hood in .NET. When I use ildasm or DotPeek i get as far as:

[SecurityCritical]
[MethodImpl(MethodImplOptions.InternalCall)]
public static string FormatDouble(double value, string format, NumberFormatInfo info);

or

IL_0008:  call string System.Number::FormatDouble(float64,
                                                  string,
                                                  class System.Globalization.NumberFormatInfo)

And can't drill further. I think if my understanding is right it's because this is a call into the CLR. I wanted to know is there any easy way to find out what the implementation is?


Solution

  • Since .NET Core is open source now, answer to this question can be updated. MethodImplOptions.InternalCall indicates FormatDouble is a FCall, which is a mechanism of communicating to native implementation. You can find the native implementation of FormatDouble here.