C# String ToString() Method

Beginner
5 min read
Updated: Nov 2026
3 examples
String

Definition

In C#, every type has a ToString() method that turns a value into text. For a string, the value already is text, so String.ToString() just returns the same string object. No new string is created.

You rarely need to call it on a variable you know is a string. It matters when the variable has a more general type, such as object: calling ToString() there runs the string's own version and gives back the text. There is also ToString(IFormatProvider), which ignores the provider for strings and returns the same instance too.

This is an instance member of System.String. Call it on a string variable, such as text.ToString(...). Calling it on a null variable throws NullReferenceException. Because strings are immutable, there is nothing to copy: ToString() on a string simply returns the same string instance.

Syntax

No arguments (or an IFormatProvider, which is ignored).

C#
string s = "hello";

string t = s.ToString();                                   // same object as s
string u = s.ToString(CultureInfo.InvariantCulture);       // still the same object
bool same = ReferenceEquals(s, t);                         // True
CallWhat it does
text.ToString()Returns text itself.
text?.ToString() ?? "(none)"Safe when text may be null.
obj.ToString()On an object that holds a string, returns that string.

ReferenceEquals(a, b) tells you whether two variables point to the very same object, not just equal text. Calling ToString() on a null string throws NullReferenceException, like any instance method.

Library declarations
C#
public override string ToString();
public string ToString(IFormatProvider? provider);

Parameters

ParameterDescription
providerOptional. A format provider. Strings ignore it, because there is nothing to format.

Return Value

Return ValueDescription
stringThe same string instance you called it on.

C# Examples

Example 1 — ToString Returns the Very Same String

Call ToString() on a string and check whether you got a copy or the same object.

C#
using System;

class Program {
    static void Main() {
        string s = "hello";
        string t = s.ToString();

        Console.WriteLine(t);
        Console.WriteLine(ReferenceEquals(s, t));
    }
}

How It Works

  1. Text in, text out. t prints hello, as expected.
  2. Same object, not a copy. ReferenceEquals checks identity, and it prints True.
  3. Why no copy is needed. Strings can never change, so sharing one object is completely safe.
  4. Different from Copy. The old string.Copy made a separate object; ToString does not.

Example 2 — Avoid the null Trap

Calling ToString() on a missing string throws. Use the null-conditional operator instead.

C#
using System;

class Program {
    static void Main() {
        string? s = null;

        Console.WriteLine(s?.ToString() ?? "(none)");

        try {
            Console.WriteLine(s.ToString());
        } catch (NullReferenceException ex) {
            Console.WriteLine(ex.GetType().Name);
        }
    }
}

How It Works

  1. A missing value. s is null, so there is no string object to ask.
  2. Safe call with ?. s?.ToString() skips the call and produces null, and ?? replaces that with (none).
  3. Unsafe call. The plain s.ToString() throws NullReferenceException; the compiler also warns about it when nullable checks are on.
  4. Simpler still. Since s is already a string, s ?? "(none)" does the same job without ToString at all.

Example 3 — Where ToString Really Helps: object Variables

Put a string, an int, and a char into an object[], then turn each one into text the same way.

C#
using System;

class Program {
    static void Main() {
        object[] items = { "a", 5, 'c' };

        foreach (object item in items) {
            Console.WriteLine(item.ToString());
        }

        object o = "text";
        Console.WriteLine(ReferenceEquals(o, o.ToString()));
    }
}

How It Works

  1. Mixed types, one variable type. The array holds a string, an int, and a char, all stored as object.
  2. Each type answers for itself. ToString() is virtual, so the int version prints 5 and the char version prints c.
  3. The string version. For "a", the string override returns the string itself.
  4. Identity again. Even through an object variable, the string returns itself, so the last line is True.

Frequently Asked Questions

It returns the same string instance it was called on. No copy is made.
No. Strings are immutable, so there is no need for a copy. ReferenceEquals(s, s.ToString()) is true.
It also returns the same instance. The provider is ignored, because a string has nothing to format.
Because the variable is null, and you cannot call an instance method on null. Use s?.ToString() or simply s ?? "".
When the value is stored in a more general type, such as object or a generic T. ToString() then returns the text no matter what the real type is.

Next: ToUpper()

Get an uppercase copy of a string using the current (or a chosen) culture.

ToUpper() tutorial →

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

13 people found this page helpful