String.ToString 方法

定义

将此实例的值转换为 String

重载

ToString()

返回 String 的此实例;不执行实际转换。

ToString(IFormatProvider)

返回 String 的此实例;不执行实际转换。

ToString()

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

返回 String 的此实例;不执行实际转换。

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

返回

当前的字符串。

示例

以下示例演示 ToString 了 方法。请注意,该示例未显式调用 ToString 方法。 而是由 复合格式设置 功能隐式调用 方法。

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
'

注解

由于此方法只是返回未更改的当前字符串,因此无需直接调用它。 它通常在复合格式设置操作中隐式调用,如示例所示。

另请参阅

适用于

ToString(IFormatProvider)

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

返回 String 的此实例;不执行实际转换。

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

参数

provider
IFormatProvider

(保留)一个对象,用于提供区域性特定的格式设置信息。

返回

当前的字符串。

实现

注解

provider 保留,当前不参与此操作。

由于此方法只是返回未更改的当前字符串,因此无需直接调用它。

适用于