ToString Method (String)
Collapse the table of content
Expand the table of content

Enum.ToString Method (String)

[ This article is for Windows Phone 8 developers. If you’re developing for Windows 10, see the latest documentation. ]

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

Namespace:  System
Assembly:  mscorlib (in mscorlib.dll)

public string ToString(
	string format
)

Parameters

format
Type: System.String
A format string.

Return Value

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

ExceptionCondition
FormatException

format contains an invalid specification.

The format parameter can contain the "G" or "g", "D" or "d", "X" or "x", and "F" or "f" format strings. If format is null 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.


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.


string shadeName = ((Shade)1).ToString("F");


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


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


Windows Phone OS

Supported in: 8.1, 8.0, 7.1, 7.0

Windows Phone

Show:
© 2017 Microsoft