Export (0) Print
Expand All

Enum.ToString Method ()

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

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

public override string ToString ()
public String ToString ()
public override function ToString () : String
Not applicable.

Return Value

The 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 [<topic://cpConFormattingOverview>].

Windows Mobile 2003 for Pocket PC, Windows Mobile 2003 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.

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

import System.*;

public class EnumSample
{
    enum Colors
    {
        Red (1),
        Blue (2);
    }//Colors

    public static void main(String[] args)
    {
        Enum myColors = Colors.Red;
        Console.WriteLine("The value of this instance is '{0}'",
            myColors.ToString());
    } //main
} //EnumSample

/*
Output.
The value of this instance is 'Red'.
*/

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, 1.0

XNA Framework

Supported in: 1.0

Community Additions

ADD
Show:
© 2014 Microsoft