String.ToString 方法
重要
一些信息与预发行产品相关,相应产品在发行之前可能会进行重大修改。 对于此处提供的信息,Microsoft 不作任何明示或暗示的担保。
将此实例的值转换为 String。
ToString() |
返回 String 的此实例;不执行实际转换。 |
ToString(IFormatProvider) |
返回 String 的此实例;不执行实际转换。 |
- Source:
- String.cs
- Source:
- String.cs
- Source:
- String.cs
返回 String 的此实例;不执行实际转换。
public:
override System::String ^ ToString();
C#
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
*/
C#
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
'
注解
由于此方法只是返回未更改的当前字符串,因此无需直接调用它。 它通常在复合格式设置操作中隐式调用,如示例所示。
另请参阅
适用于
.NET 9 和其他版本
产品 | 版本 |
---|---|
.NET | Core 1.0, Core 1.1, Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9 |
.NET Framework | 1.1, 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1 |
.NET Standard | 1.0, 1.1, 1.2, 1.3, 1.4, 1.5, 1.6, 2.0, 2.1 |
UWP | 10.0 |
- Source:
- String.cs
- Source:
- String.cs
- Source:
- String.cs
返回 String 的此实例;不执行实际转换。
public:
virtual System::String ^ ToString(IFormatProvider ^ provider);
C#
public string ToString (IFormatProvider? provider);
C#
public string ToString (IFormatProvider provider);
override this.ToString : IFormatProvider -> string
Public Function ToString (provider As IFormatProvider) As String
参数
- provider
- IFormatProvider
(保留)一个对象,用于提供区域性特定的格式设置信息。
返回
当前的字符串。
实现
注解
provider
保留,当前不参与此操作。
由于此方法只是返回未更改的当前字符串,因此无需直接调用它。
适用于
.NET 9 和其他版本
产品 | 版本 |
---|---|
.NET | Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9 |
.NET Framework | 1.1, 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1 |
.NET Standard | 2.0, 2.1 |