Export (0) Print
Expand All

Enum.ToString Method

Converts the value of this instance to its equivalent string representation.

Overload List

Converts the value of this instance to its equivalent string representation.

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;

Converts the value of this instance to its equivalent string representation using the specified format information.

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

Converts the value of this instance to its equivalent string representation using the specified format.

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

Converts the value of this instance to its equivalent string representation using the specified format and format information.

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

Example

[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] 
' Sample for Enum.ToString(String)
Imports System

Class Sample
   Enum Colors
      Red
      Green
      Blue
      Yellow = 12
   End Enum 'Colors
   
   Public Shared Sub Main()
      Dim myColor As Colors = Colors.Yellow
      
      Console.WriteLine("Colors.Red = {0}", Colors.Red.ToString("d"))
      Console.WriteLine("Colors.Green = {0}", Colors.Green.ToString("d"))
      Console.WriteLine("Colors.Blue = {0}", Colors.Blue.ToString("d"))
      Console.WriteLine("Colors.Yellow = {0}", Colors.Yellow.ToString("d"))
      
      Console.WriteLine("{0}myColor = Colors.Yellow{0}", Environment.NewLine)
      
      Console.WriteLine("myColor.ToString(""g"") = {0}", myColor.ToString("g"))
      Console.WriteLine("myColor.ToString(""G"") = {0}", myColor.ToString("G"))
      
      Console.WriteLine("myColor.ToString(""x"") = {0}", myColor.ToString("x"))
      Console.WriteLine("myColor.ToString(""X"") = {0}", myColor.ToString("X"))
      
      Console.WriteLine("myColor.ToString(""d"") = {0}", myColor.ToString("d"))
      Console.WriteLine("myColor.ToString(""D"") = {0}", myColor.ToString("D"))
      
      Console.WriteLine("myColor.ToString(""f"") = {0}", myColor.ToString("f"))
      Console.WriteLine("myColor.ToString(""F"") = {0}", myColor.ToString("F"))
   End Sub 'Main
End Class 'Sample
'
'This example produces the following results:
'
'Colors.Red = 0
'Colors.Green = 1
'Colors.Blue = 2
'Colors.Yellow = 12
'
'myColor = Colors.Yellow
'
'myColor.ToString("g") = Yellow
'myColor.ToString("G") = Yellow
'myColor.ToString("x") = 0000000C
'myColor.ToString("X") = 0000000C
'myColor.ToString("d") = 12
'myColor.ToString("D") = 12
'myColor.ToString("f") = Yellow
'myColor.ToString("F") = Yellow
'

[C#] 
// Sample for Enum.ToString(String)
using System;

class Sample 
{
    enum Colors {Red, Green, Blue, Yellow = 12};

    public static void Main() 
    {
    Colors myColor = Colors.Yellow;

    Console.WriteLine("Colors.Red = {0}", Colors.Red.ToString("d"));
    Console.WriteLine("Colors.Green = {0}", Colors.Green.ToString("d"));
    Console.WriteLine("Colors.Blue = {0}", Colors.Blue.ToString("d"));
    Console.WriteLine("Colors.Yellow = {0}", Colors.Yellow.ToString("d"));

    Console.WriteLine("{0}myColor = Colors.Yellow{0}", Environment.NewLine);

    Console.WriteLine("myColor.ToString(\"g\") = {0}", myColor.ToString("g"));
    Console.WriteLine("myColor.ToString(\"G\") = {0}", myColor.ToString("G"));

    Console.WriteLine("myColor.ToString(\"x\") = {0}", myColor.ToString("x"));
    Console.WriteLine("myColor.ToString(\"X\") = {0}", myColor.ToString("X"));

    Console.WriteLine("myColor.ToString(\"d\") = {0}", myColor.ToString("d"));
    Console.WriteLine("myColor.ToString(\"D\") = {0}", myColor.ToString("D"));    

    Console.WriteLine("myColor.ToString(\"f\") = {0}", myColor.ToString("f"));
    Console.WriteLine("myColor.ToString(\"F\") = {0}", myColor.ToString("F"));
    }
}
/*
This example produces the following results:
Colors.Red = 0
Colors.Green = 1
Colors.Blue = 2
Colors.Yellow = 12

myColor = Colors.Yellow

myColor.ToString("g") = Yellow
myColor.ToString("G") = Yellow
myColor.ToString("x") = 0000000C
myColor.ToString("X") = 0000000C
myColor.ToString("d") = 12
myColor.ToString("D") = 12
myColor.ToString("f") = Yellow
myColor.ToString("F") = Yellow
*/

[C++] 
// Sample for Enum::ToString(String)
#using <mscorlib.dll>

using namespace System;

__value enum Colors {Red, Green, Blue, Yellow = 12};

int main() {
   Colors myColor = Colors::Yellow;

   Console::WriteLine(S"Colors::Red = {0}", __box(Colors::Red)->ToString(S"d"));
   Console::WriteLine(S"Colors::Green = {0}",__box( Colors::Green)->ToString(S"d"));
   Console::WriteLine(S"Colors::Blue = {0}", __box(Colors::Blue)->ToString(S"d"));
   Console::WriteLine(S"Colors::Yellow = {0}", __box(Colors::Yellow)->ToString(S"d"));

   Console::WriteLine(S" {0}myColor = Colors::Yellow {0}", Environment::NewLine);

   Console::WriteLine(S"myColor->ToString(\"g\") = {0}", __box(myColor)->ToString(S"g"));
   Console::WriteLine(S"myColor->ToString(\"G\") = {0}", __box(myColor)->ToString(S"G"));

   Console::WriteLine(S"myColor->ToString(\"x\") = {0}", __box(myColor)->ToString(S"x"));
   Console::WriteLine(S"myColor->ToString(\"X\") = {0}", __box(myColor)->ToString(S"X"));

   Console::WriteLine(S"myColor->ToString(\"d\") = {0}", __box(myColor)->ToString(S"d"));
   Console::WriteLine(S"myColor->ToString(\"D\") = {0}", __box(myColor)->ToString(S"D"));

   Console::WriteLine(S"myColor->ToString(\"f\") = {0}", __box(myColor)->ToString(S"f"));
   Console::WriteLine(S"myColor->ToString(\"F\") = {0}", __box(myColor)->ToString(S"F"));
}

/*
This example produces the following results:
Colors::Red = 0
Colors::Green = 1
Colors::Blue = 2
Colors::Yellow = 12

myColor = Colors::Yellow

myColor->ToString("g") = Yellow
myColor->ToString("G") = Yellow
myColor->ToString("x") = 0000000C
myColor->ToString("X") = 0000000C
myColor->ToString("d") = 12
myColor->ToString("D") = 12
myColor->ToString("f") = Yellow
myColor->ToString("F") = Yellow
*/

[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

Enum Class | Enum Members | System Namespace

Show:
© 2014 Microsoft