Click to Rate and Give Feedback
MSDN
MSDN Library
.NET Development
.NET Framework 3.5
System Namespace
Enum Class
Enum Methods
ToString Method
 ToString Method
Collapse All/Expand All Collapse All
This page is specific to
Microsoft Visual Studio 2008/.NET Framework 3.5

Other versions are also available for the following:
.NET Framework Class Library
Enum..::.ToString Method

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
Visual Basic (Usage)
Dim instance As Enum
Dim returnValue As String

returnValue = instance.ToString()
C#
public override string ToString()
Visual C++
public:
virtual String^ ToString() override
JScript
public override function ToString() : String

Return Value

Type: System..::.String
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 Formatting Overview.

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.

Visual Basic
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'.
C#
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'.
*/
Visual C++
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
Tags What's this?: Add a tag
Community Content   What is Community Content?
Add new content RSS  Annotations
Processing
© 2009 Microsoft Corporation. All rights reserved. Terms of Use | Trademarks | Privacy Statement | Site Feedback
Page view tracker