Export (0) Print
Expand All
0 out of 2 rated this helpful - Rate this topic

String.ToString Method

Converts the value of this instance to a String.

Overload List

Returns this instance of String; no actual conversion is performed.

Supported by the .NET Compact Framework.

[Visual Basic] Overloads Overrides Public Function ToString() As String
[C#] public override string ToString();
[C++] public: String* ToString();
[JScript] public override function ToString() : String;

Returns this instance of String; no actual conversion is performed.

Supported by the .NET Compact Framework.

[Visual Basic] Overloads Public Overridable Function ToString(IFormatProvider) As String Implements IConvertible.ToString
[C#] public virtual string ToString(IFormatProvider);
[C++] public: virtual String* ToString(IFormatProvider*);
[JScript] public function ToString(IFormatProvider) : String;

Example

[Visual Basic, C#, C++] The following example demonstrates the ToString method.

[Visual Basic, C#, C++] Note   This example shows how to use one of the overloaded versions of ToString. For other examples that might be available, see the individual overload topics.
[Visual Basic] 
Imports System
 _

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 'Main
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
'

[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
*/

[C++] 
#using <mscorlib.dll>

using namespace System;

int main()
{
   String* str1 = S"123";
   String* str2 = S"abc";

   Console::WriteLine(S"Original str1: {0}", str1);
   Console::WriteLine(S"Original str2: {0}", str2);
   Console::WriteLine(S"str1 same as str2?: {0}", __box(Object::ReferenceEquals(str1, str2)));

   str2 = str1;
   Console::WriteLine();
   Console::WriteLine(S"New str2:      {0}", str2);
   Console::WriteLine(S"str1 same as str2?: {0}", __box(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
*/

[JScript] No example is available for JScript. To view a Visual Basic, C#, or C++ example, click the Language Filter button Language Filter in the upper-left corner of the page.

See Also

String Class | String Members | System Namespace

Did you find this helpful?
(1500 characters remaining)
Thank you for your feedback
Show:
© 2014 Microsoft. All rights reserved.