String.ToString Method

Definition

Converts the value of this instance to a String.

Overloads

ToString()

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

ToString(IFormatProvider)

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

ToString()

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

public:
 override System::String ^ ToString();
public override string ToString ();
override this.ToString : unit -> string
Public Overrides Function ToString () As String

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 namespace System;
int 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;
   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
*/
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
*/
open System

[<EntryPoint>]
let main _ =
    let str1 = "123"
    let str2 = "abc"
    printfn $"Original str1: {str1}"
    printfn $"Original str2: {str2}"
    printfn $"str1 same as str2?: {Object.ReferenceEquals(str1, str2)}"

    let str2 = str1.ToString()
    printfn $"\nNew str2:      {str2}"
    printfn $"str1 same as str2?: {Object.ReferenceEquals(str1, str2)}"
    0

(*
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
*)
 _

Class stringToString
   
   Public Shared Sub Main()
      Dim str1 As [String] = "123"
      Dim str2 As [String] = "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))
   End Sub
End Class
'
'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

ToString(IFormatProvider)

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

public:
 virtual System::String ^ ToString(IFormatProvider ^ provider);
public string ToString (IFormatProvider? provider);
public string ToString (IFormatProvider provider);
override this.ToString : IFormatProvider -> string
Public Function ToString (provider As IFormatProvider) As String

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