Export (0) Print
Expand All

Enum.ToString Method (String)

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
)
public String ToString (
	String format
)
public function ToString (
	format : String
) : String
Not applicable.

Parameters

format

A format string.

Return Value

The string representation of the value of this instance as specified by format.

Exception typeCondition

FormatException

format contains an invalid specification.

The format parameter can contain format characters "G" or "g", "D" or "d", "X" or "x", and "F" or "f". If format is a 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 [<topic://cpConFormattingOverview>].

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

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

// Sample for Enum.ToString(String)
import System.*;

class Sample
{
    enum Colors
    {
        red (0),
        green (1),
        blue (2),
        yellow (12);
    } //Colors

    public static void main(String[] args)
    {
        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.get_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"));
    } //main
} //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
*/

Windows 98, Windows Server 2000 SP4, Windows CE, Windows Millennium Edition, Windows Mobile for Pocket PC, Windows Mobile for Smartphone, Windows Server 2003, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP SP2, Windows XP Starter Edition

The Microsoft .NET Framework 3.0 is supported on Windows Vista, Microsoft Windows XP SP2, and Windows Server 2003 SP1.

.NET Framework

Supported in: 3.0, 2.0, 1.1, 1.0

.NET Compact Framework

Supported in: 2.0

XNA Framework

Supported in: 1.0

Community Additions

ADD
Show:
© 2015 Microsoft