Updated: August 2009
Converts the value of this instance to its equivalent string representation.
Namespace:
System
Assembly:
mscorlib (in mscorlib.dll)
Visual Basic (Declaration)
Public Overrides Function ToString As String
Dim instance As Enum
Dim returnValue As String
returnValue = instance.ToString()
public override string ToString()
public:
virtual String^ ToString() override
public override function ToString() : String
Return Value
Type:
System..::.StringThe string representation of the value of this instance.
This method works as if the general format character, "G", were specified. That is, if the FlagsAttribute is not applied to this enumerated type and there is a named constant equal to the value of this instance, then the return value is a string containing the name of the constant. If the FlagsAttribute is applied and there is a combination of one or more named constants equal to the value of this instance, then the return value is a string containing a delimiter-separated list of the names of the constants. Otherwise, the return value is the string representation of the numeric value of this instance.
For more information about 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()
string shadeName = ((Shade) 1).ToString();
Windows Mobile for Pocket PC, Windows Mobile for Smartphone, Windows CE Platform Note: Because this method searches metadata tables, it heavily uses system resources and can impede performance.
The following example demonstrates converting an enumerated value to a string.
Imports System
Public Class EnumSample
Enum Colors
Red = 1
Blue = 2
End Enum
Public Shared Sub Main()
Dim myColors As Colors = Colors.Red
Console.WriteLine("The value of this instance is '{0}'", _
myColors.ToString())
End Sub
End Class
'Output.
'The value of this instance is 'Red'.
using System;
public class EnumSample {
enum Colors {Red = 1, Blue = 2};
public static void Main() {
Enum myColors = Colors.Red;
Console.WriteLine("The value of this instance is '{0}'",
myColors.ToString());
}
}
/*
Output.
The value of this instance is 'Red'.
*/
using namespace System;
public ref class EnumSample
{
public:
enum class Colors
{
Red = 1,
Blue = 2
};
static void main()
{
Enum ^ myColors = Colors::Red;
Console::WriteLine( "The value of this instance is '{0}'", myColors );
}
};
int main()
{
EnumSample::main();
}
/*
Output.
The value of this instance is 'Red'.
*/
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, 1.0
XNA Framework
Supported in: 3.0, 2.0, 1.0
Reference
Date | History | Reason |
|---|
August 2009
| Added the Notes for Callers section. |
Customer feedback.
|