简体   繁体   中英

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?

What about looking at mono source code? Mono simply use IL to do the whole thing without any C code.

mcs/class/corlib/System/Double.cs
mcs/class/corlib/System/NumberFormatter.cs

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 .

我认为 WinDbg 会有所帮助 - 您是否检查了单声道的实现?

在 .NET 5 中, FormatDouble现在在 C# 中实现,源代码可在dotnet/runtime 存储库中找到

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM