다음을 통해 공유


Enum.ToString Method (String)

Microsoft Silverlight will reach end of support after October 2021. Learn more.

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)

Syntax

'Declaration
Public Function ToString ( _
    format As String _
) As String
public string ToString(
    string format
)

Parameters

Return Value

Type: System.String
The string representation of the value of this instance as specified by format.

Exceptions

Exception Condition
FormatException

format contains an invalid specification.

Remarks

The format parameter can contain the "G" or "g", "D" or "d", "X" or "x", and "F" or "f" format strings. If format is nulla null reference (Nothing in Visual Basic) or an empty string (""), the general format specifier ("G") is used. For more information about formatting enumeration values, see Enumeration Format Strings. For more information about formatting in general, see Formatting Types.

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");

Examples

The following example demonstrates how to convert an enumerated value to a string.

' Sample for Enum.ToString(String)

Class Example
   Enum Colors
      Red
      Green
      Blue
      Yellow = 12
   End Enum 'Colors

   Public Shared Sub Demo(ByVal outputBlock As System.Windows.Controls.TextBlock)
      Dim myColor As Colors = Colors.Yellow

      outputBlock.Text &= String.Format("Colors.Red = {0}", Colors.Red.ToString("d")) & vbCrLf
      outputBlock.Text &= String.Format("Colors.Green = {0}", Colors.Green.ToString("d")) & vbCrLf
      outputBlock.Text &= String.Format("Colors.Blue = {0}", Colors.Blue.ToString("d")) & vbCrLf
      outputBlock.Text &= String.Format("Colors.Yellow = {0}", Colors.Yellow.ToString("d")) & vbCrLf

      outputBlock.Text &= String.Format("{0}myColor = Colors.Yellow{0}", vbCrLf) & vbCrLf

      outputBlock.Text &= String.Format("myColor.ToString(""g"") = {0}", myColor.ToString("g")) & vbCrLf
      outputBlock.Text &= String.Format("myColor.ToString(""G"") = {0}", myColor.ToString("G")) & vbCrLf

      outputBlock.Text &= String.Format("myColor.ToString(""x"") = {0}", myColor.ToString("x")) & vbCrLf
      outputBlock.Text &= String.Format("myColor.ToString(""X"") = {0}", myColor.ToString("X")) & vbCrLf

      outputBlock.Text &= String.Format("myColor.ToString(""d"") = {0}", myColor.ToString("d")) & vbCrLf
      outputBlock.Text &= String.Format("myColor.ToString(""D"") = {0}", myColor.ToString("D")) & vbCrLf

      outputBlock.Text &= String.Format("myColor.ToString(""f"") = {0}", myColor.ToString("f")) & vbCrLf
      outputBlock.Text &= String.Format("myColor.ToString(""F"") = {0}", myColor.ToString("F")) & vbCrLf
   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 Example
{
   enum Colors { Red, Green, Blue, Yellow = 12 };

   public static void Demo(System.Windows.Controls.TextBlock outputBlock)
   {
      Colors myColor = Colors.Yellow;

      outputBlock.Text += String.Format("Colors.Red = {0}", Colors.Red.ToString("d")) + "\n";
      outputBlock.Text += String.Format("Colors.Green = {0}", Colors.Green.ToString("d")) + "\n";
      outputBlock.Text += String.Format("Colors.Blue = {0}", Colors.Blue.ToString("d")) + "\n";
      outputBlock.Text += String.Format("Colors.Yellow = {0}", Colors.Yellow.ToString("d")) + "\n";

      outputBlock.Text += String.Format("{0}myColor = Colors.Yellow{0}", "\n") + "\n";

      outputBlock.Text += String.Format("myColor.ToString(\"g\") = {0}", myColor.ToString("g")) + "\n";
      outputBlock.Text += String.Format("myColor.ToString(\"G\") = {0}", myColor.ToString("G")) + "\n";

      outputBlock.Text += String.Format("myColor.ToString(\"x\") = {0}", myColor.ToString("x")) + "\n";
      outputBlock.Text += String.Format("myColor.ToString(\"X\") = {0}", myColor.ToString("X")) + "\n";

      outputBlock.Text += String.Format("myColor.ToString(\"d\") = {0}", myColor.ToString("d")) + "\n";
      outputBlock.Text += String.Format("myColor.ToString(\"D\") = {0}", myColor.ToString("D")) + "\n";

      outputBlock.Text += String.Format("myColor.ToString(\"f\") = {0}", myColor.ToString("f")) + "\n";
      outputBlock.Text += String.Format("myColor.ToString(\"F\") = {0}", myColor.ToString("F")) + "\n";
   }
}
/*
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
*/

Version Information

Silverlight

Supported in: 5, 4, 3

Silverlight for Windows Phone

Supported in: Windows Phone OS 7.1, Windows Phone OS 7.0

XNA Framework

Supported in: Xbox 360, Windows Phone OS 7.0

Platforms

For a list of the operating systems and browsers that are supported by Silverlight, see Supported Operating Systems and Browsers.

Change History

Date

History

Reason

August 2009

Added the Notes for Callers section.

Customer feedback.