String.Format () Hex with a leading zero does not work for the second argument

I am having a strange problem with String.Format(). I need to format two hexadecimal numbers with leading zeros to enter up to 8 digits. However, it only works for the first argument ( {0:X8}). For the second argument ( {1:X8}), only "X8" is printed.

This is my code:

public struct DataDirectory
{
    public uint VirtualAddress
    {
        get;
        internal set;
    }

    public uint Size
    {
        get;
        internal set;
    }

    public override string ToString()
    {
        return String.Format("{{VirtualAddress=0x{0:X8},Size=0x{1:X8}}}", VirtualAddress, Size);
    }
}

The debugger output prints this:

Debugger output

EDIT: It seems to work if I remove the curly braces at the beginning and end of the format string, but then I miss the lines that are returned from ToString()(the debugger still adds them to the QuickWatch window):

return String.Format("VirtualAddress=0x{0:X8},Size=0x{1:X8}", VirtualAddress, Size);

Is it possible to format two hexadecimal numbers with String.Format()?

+4
1

}}}. , Size=0x{1:X8}{2}. :

String.Format("{{VirtualAddress=0x{0:X8},Size=0x{1:X8}{2}", 
  VirtualAddress, Size, "}"
  )

{1:X8}}} , , }} }, } . Size.ToString("X8}"), , .

, :

String.Format("{2}VirtualAddress=0x{0:X8},Size=0x{1:X8}{3}", 
  VirtualAddress, Size, "{", "}"
  )
+4

All Articles