Updated: August 2009
Converts the value of this instance to its equivalent string representation using the specified format.
Namespace:
System
Assembly:
mscorlib (in mscorlib.dll)
Visual Basic (Declaration)
Public Function ToString ( _
format As String _
) As String
Dim instance As Enum
Dim format As String
Dim returnValue As String
returnValue = instance.ToString(format)
public string ToString(
string format
)
public:
String^ ToString(
String^ format
)
public function ToString(
format : String
) : String
Return Value
Type:
System..::.StringThe string representation of the value of this instance as specified by format.
The format parameter can contain format characters "G" or "g", "D" or "d", "X" or "x", and "F" or "f". If format is nullNothingnullptra null reference (Nothing in Visual Basic) or an empty string (""), the general format specifier ("G") is used. For more information about these format characters, see the Remarks section of the Format method. For more information about formatting in general, see Formatting Overview.
Notes to Callers: If multiple enumeration members have the same underlying value and you attempt to retrieve the string representation of an enumeration member's name based on its underlying value, your code should not make any assumptions about which name the method will return. For example, the following enumeration defines two members, Shade.Gray and Shade.Grey, that have the same underlying value.
Public Enum Shade
White = 0
Gray = 1
Grey = 1
Black = 2
End Enum
enum Shade
{
White = 0, Gray = 1, Grey = 1, Black = 2
}
The following method call attempts to retrieve the name of a member of the Shade enumeration whose underlying value is 1. The method can return either "Gray" or "Grey", and your code should not make any assumptions about which string will be returned.
Dim shadeName As String = CType(1, Shade).ToString("F")
string shadeName = ((Shade) 1).ToString("F");
The following example demonstrates how to convert an enumerated value to a string.
' 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
'
// 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
*/
// Sample for Enum::ToString(String)
using namespace System;
enum class Colors
{
Red, Green, Blue, Yellow = 12
};
int 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
*/
Windows 7, Windows Vista, Windows XP SP2, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP Starter Edition, Windows Server 2008 R2, Windows Server 2008, Windows Server 2003, Windows Server 2000 SP4, Windows Millennium Edition, Windows 98, Windows CE, Windows Mobile for Smartphone, Windows Mobile for Pocket PC, Xbox 360, Zune
The .NET Framework and .NET Compact Framework do not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.
.NET Framework
Supported in: 3.5, 3.0, 2.0, 1.1, 1.0
.NET Compact Framework
Supported in: 3.5, 2.0
XNA Framework
Supported in: 3.0, 2.0, 1.0
Reference
Other Resources
Date | History | Reason |
|---|
August 2009
| Added the Notes for Callers section. |
Customer feedback.
|
April 2009
| Added exception information. |
Information enhancement.
|