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
Call
What 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
Parameter
Description
provider
Optional. A format provider. Strings ignore it, because there is nothing to format.
Return Value
Return Value
Description
string
The 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));
}
}
Output
hello
True
How It Works
Text in, text out.t prints hello, as expected.
Same object, not a copy.ReferenceEquals checks identity, and it prints True.
Why no copy is needed. Strings can never change, so sharing one object is completely safe.
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);
}
}
}
Output
(none)
NullReferenceException
How It Works
A missing value.s is null, so there is no string object to ask.
Safe call with ?.s?.ToString() skips the call and produces null, and ?? replaces that with (none).
Unsafe call. The plain s.ToString() throws NullReferenceException; the compiler also warns about it when nullable checks are on.
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()));
}
}
Output
a
5
c
True
How It Works
Mixed types, one variable type. The array holds a string, an int, and a char, all stored as object.
Each type answers for itself.ToString() is virtual, so the int version prints 5 and the char version prints c.
The string version. For "a", the string override returns the string itself.
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.