using System.Globalization;
public static void Main()
IntPtr ptr = new IntPtr(0xff);
Console.WriteLine(ptr.ToString());
Console.WriteLine(ptr.ToString("x"));
Console.WriteLine($"0x{ptr.ToString("x")}");
Console.WriteLine("0x{0:x}", ptr);
Console.WriteLine($"0x{ptr:x}");
Console.WriteLine(IntPtr.Zero is IFormattable);
Console.WriteLine($"{new MyTest():n}");
Console.WriteLine($"{new MyTest2():n}");
Console.WriteLine($"{new MyTest2()}");
Console.WriteLine($"0x{(int)ptr:x}");
public override string ToString()
public class MyTest2 : IFormattable
public override string ToString()
public string ToString(string format)
return this.ToString(format, CultureInfo.CurrentCulture);
public string ToString(string format, IFormatProvider provider)