String.ToString String.ToString String.ToString String.ToString Method

定義

將這個執行個體的值轉換為 StringConverts the value of this instance to a String.

多載

ToString(IFormatProvider) ToString(IFormatProvider) ToString(IFormatProvider) ToString(IFormatProvider)

傳回這個 String 執行個體;不會實際執行轉換。Returns this instance of String; no actual conversion is performed.

ToString() ToString() ToString() ToString()

傳回這個 String 執行個體;不會實際執行轉換。Returns this instance of String; no actual conversion is performed.

ToString(IFormatProvider) ToString(IFormatProvider) ToString(IFormatProvider) 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 IFormatProvider IFormatProvider 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() ToString() ToString() 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 'Sample
'
'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.

另請參閱

適用於