String.ToString 方法

定义

将此实例的值转换为 StringConverts the value of this instance to a String.

重载

ToString(IFormatProvider)

返回 String 的此实例;不执行实际转换。Returns this instance of String; no actual conversion is performed.

ToString()

返回 String 的此实例;不执行实际转换。Returns this instance of String; no actual conversion is performed.

ToString(IFormatProvider)

返回 String 的此实例;不执行实际转换。Returns this instance of String; no actual conversion is performed.

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

参数

provider
IFormatProvider

(保留)一个对象,用于提供区域性特定的格式设置信息。(Reserved) An object that supplies culture-specific formatting information.

返回

当前的字符串。The current string.

实现

注解

provider 保留,并且当前不参与此操作。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.

ToString()

返回 String 的此实例;不执行实际转换。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

返回

当前的字符串。The current string.

示例

下面的示例演示 ToString 方法。请注意,该示例不显式调用 ToString 方法。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
*/
 _

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
'

注解

由于此方法只是返回未更改的当前字符串,因此不需要直接调用它。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.

另请参阅

适用于