String.ToString Método

Definición

Convierte el valor de la instancia en un objeto String.

Sobrecargas

ToString()

Devuelve la instancia de String; no se efectúa una conversión real.

ToString(IFormatProvider)

Devuelve la instancia de String; no se efectúa una conversión real.

ToString()

Devuelve la instancia de String; no se efectúa una conversión real.

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

Devoluciones

Cadena actual.

Ejemplos

En el ejemplo siguiente se muestra el ToString método . Tenga en cuenta que el ejemplo no llama explícitamente al ToString método . En su lugar, la característica de formato compuesto llama implícitamente al método .

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
'

Comentarios

Dado que este método simplemente devuelve la cadena actual sin cambios, no es necesario llamarla directamente. Normalmente se llama implícitamente en una operación de formato compuesto, como se muestra en el ejemplo.

Consulte también

Se aplica a

ToString(IFormatProvider)

Devuelve la instancia de String; no se efectúa una conversión real.

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

Parámetros

provider
IFormatProvider

(Reservado) Un objeto que aporta información de formato específica de la referencia cultural.

Devoluciones

Cadena actual.

Implementaciones

Comentarios

provider está reservado y no participa actualmente en esta operación.

Dado que este método simplemente devuelve la cadena actual sin cambios, no es necesario llamarla directamente.

Se aplica a