IntPtr into hex string in string.Format

c#, hex, intptr, tostring

Solution

For the `String.Format` method to use a format string for an argument, the argument has to implement the `IFormattable` interface.

As the `IntPtr` type doesn't implement `IFormattable`, the `String.Format` method will just call the parameterless `ToString` method to turn the `IntPtr` value into a string.

Problem

Note, I am not quite sure this question belongs to this site but I try to be constructive. Why does the code ``` IntPtr ptr = new IntPtr(1234); Console.WriteLine(string.Format("{0:X8}", ptr)); Console.WriteLine(ptr.ToString("X8")); Console.WriteLine(string.Format("{0:X8}", ptr.ToInt32())); ``` output ``` 1234 000004D2 000004D2 ``` Why isn't the hex formatting applied to the `IntPtr` argument directly, when requested so in formatting? Are there any known arguments against designing the functionality so - ? If there are no arguments against such design then by which channel should I report this issue to Microsoft? Note also, that the debugger displays the `IntPtr` value in hex if requested so. I find that it would be quite intuitive and relatively frequent way of printing out `IntPtr` values. I also have found code written by other people who used the first line and obviously expected the hex result, but actually the result was different. It also took some time for me to notice the issue, which complicated understanding the log messages.

Original source