String.ToString Method

Definition

Converts the value of this instance to a String.

Overloads

ToString(IFormatProvider)

Returns this instance of String; no actual conversion is performed.

ToString()

Returns this instance of String; no actual conversion is performed.

ToString(IFormatProvider)

Source:
String.cs
Source:
String.cs
Source:
String.cs

Returns this instance of String; no actual conversion is performed.

public string ToString (IFormatProvider? provider);
public string ToString (IFormatProvider provider);

Parameters

provider
IFormatProvider

(Reserved) An object that supplies culture-specific formatting information.

Returns

The current string.

Implements

Remarks

provider is reserved, and does not currently participate in this operation.

Because this method simply returns the current string unchanged, there is no need to call it directly.

Applies to

.NET 9 and other versions
Product Versions
.NET Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9
.NET Framework 1.1, 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
.NET Standard 2.0, 2.1

ToString()

Source:
String.cs
Source:
String.cs
Source:
String.cs

Returns this instance of String; no actual conversion is performed.

public override string ToString ();

Returns

The current string.

Examples

The following example demonstrates the ToString method.Note that the example does not explicitly call the ToString method. Instead, the method is called implicitly by the composite formatting feature.

using System;

class stringToString {
    public static void Main() {
    String str1 = "123";
    String str2 = "abc";

    Console.WriteLine("Original str1: {0}", str1);
    Console.WriteLine("Original str2: {0}", str2);
    Console.WriteLine("str1 same as str2?: {0}", Object.ReferenceEquals(str1, str2));

    str2 = str1.ToString();
    Console.WriteLine();
    Console.WriteLine("New str2:      {0}", str2);
    Console.WriteLine("str1 same as str2?: {0}", Object.ReferenceEquals(str1, str2));
    }
}
/*
This code produces the following output:
Original str1: 123
Original str2: abc
str1 same as str2?: False

New str2:      123
str1 same as str2?: True
*/

Remarks

Because this method simply returns the current string unchanged, there is no need to call it directly. It is usually called implicitly in a composite formatting operation, as the example shows.

See also

Applies to

.NET 9 and other versions
Product Versions
.NET Core 1.0, Core 1.1, Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9
.NET Framework 1.1, 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
.NET Standard 1.0, 1.1, 1.2, 1.3, 1.4, 1.5, 1.6, 2.0, 2.1
UWP 10.0