C# Reference ยท Classes and Object-Oriented Concepts
ToString()
Reference for ToString() in C#: the default output, overriding it, IFormattable format strings, interpolation, records and what debuggers display.
Object.ToString() returns a string that represents the object. It is called implicitly by Console.WriteLine(obj), by string concatenation, by string interpolation and by string.Format. Unless you override it, a class prints its full type name, which is rarely useful. A good override returns a short, human-readable summary that helps in logs and the debugger.
Types that need several output shapes, such as numbers, dates or your own Money, implement IFormattable so callers can write value.ToString("F2", culture) or $"{value:F2}".
public override string ToString() => $"{Name} ({Age})";
Try it
Press Run (or Ctrl + Enter) to see the result here.
Program input ยท what you'd type while the program runs
Type the answers before you press Run, one per line. Each time the program reads a line (with Console.ReadLine()), it gets the next line from this box.
Press Run (or Ctrl + Enter) to see the result here.
Program input ยท what you'd type while the program runs
Type the answers before you press Run, one per line. Each time the program reads a line (with Console.ReadLine()), it gets the next line from this box.
Syntax
public virtual string? ToString(); // System.Object public interface IFormattable { string ToString(string? format, IFormatProvider? formatProvider); }
| Part | Description |
|---|---|
ToString() |
Parameterless version. Override it for a default text form. |
format |
A format string such as "F2", "d" or your own codes. null or empty means "default". |
formatProvider |
Supplies culture-specific symbols. In this site's runner globalization is invariant, so use CultureInfo.InvariantCulture. |
Returns: the text. A well-behaved ToString() never returns null and does not throw.
Usage notes
- The default output is the type name. For a nested or top-level class in this runner it prints the simple name, for example
Plain; anint[]printsSystem.Int32[], as the first example shows. - Interpolation and concatenation call
ToString()(orIFormattable.ToStringwhen you add:format).nullbecomes an empty string, not an exception. - Records generate a
ToStringthat looks likePoint { X = 3, Y = 4 }(see the second example). You can seal or replace it. - Keep it cheap and side-effect free. Debuggers, loggers and exception messages call it at awkward moments. Avoid I/O and locking.
- Do not use it for machine-readable data. Serialize with
System.Text.Jsonor a documented format. Human strings change. - Culture matters for numbers and dates.
1234.5.ToString()may use a comma on some systems. PassCultureInfo.InvariantCulturefor logs and files. - Sensitive data. Do not put passwords or tokens in
ToString(); it leaks into logs. - Debugger text is separate.
[DebuggerDisplay("...")]changes what the debugger shows without touchingToString(). - Value types: overriding
ToString()on a struct avoids the default reflection-based text fromValueType.