Skip to content
Tutorials Center
My learning

Topics

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}".

C#
public override string ToString() => $"{Name} ({Age})";

Try it

C# Default and overridden ToString

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.

C# IFormattable and records

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

C#
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; an int[] prints System.Int32[], as the first example shows.
  • Interpolation and concatenation call ToString() (or IFormattable.ToString when you add :format). null becomes an empty string, not an exception.
  • Records generate a ToString that looks like Point { 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.Json or a documented format. Human strings change.
  • Culture matters for numbers and dates. 1234.5.ToString() may use a comma on some systems. Pass CultureInfo.InvariantCulture for 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 touching ToString().
  • Value types: overriding ToString() on a struct avoids the default reflection-based text from ValueType.