Enum.ToString Method

Definition

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

Overloads

ToString(String, IFormatProvider)
Obsolete.
Obsolete.

This method overload is obsolete; use ToString(String).

ToString()

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

ToString(String)

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

ToString(IFormatProvider)
Obsolete.
Obsolete.

This method overload is obsolete; use ToString().

ToString(String, IFormatProvider)

Source:
Enum.cs
Source:
Enum.cs
Source:
Enum.cs

Caution

The provider argument is not used. Please use ToString(String).

Caution

The provider argument is not used. Use ToString(String) instead.

This method overload is obsolete; use ToString(String).

[System.Obsolete("The provider argument is not used. Please use ToString(String).")]
public string ToString (string? format, IFormatProvider? provider);
[System.Obsolete("The provider argument is not used. Use ToString(String) instead.")]
public string ToString (string? format, IFormatProvider? provider);
[System.Obsolete("The provider argument is not used. Please use ToString(String).")]
public string ToString (string format, IFormatProvider provider);
public string ToString (string format, IFormatProvider provider);

Parameters

format
String

A format specification.

provider
IFormatProvider

(Obsolete.)

Returns

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

Implements

Attributes

Exceptions

format does not contain a valid format specification.

format equals "X", but the enumeration type is unknown.

Remarks

The format parameter can be one of the following format strings: "G" or "g", "D" or "d", "X" or "x", and "F" or "f" (the format string is not case-sensitive). If format is null or an empty string (""), the general format specifier ("G") is used. For more information about the enumeration format strings and formatting enumeration values, see Enumeration Format Strings. For more information about formatting in general, see Formatting Types.

Specify only format; the provider parameter is obsolete.

See also

Applies to

.NET 9 and other versions
Product Versions (Obsolete)
.NET (Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9)
.NET Framework 1.1 (2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7 (package-provided), 4.7, 4.7.1 (package-provided), 4.7.1, 4.7.2 (package-provided), 4.7.2, 4.8 (package-provided), 4.8, 4.8.1)
.NET Standard (2.0, 2.1)

ToString()

Source:
Enum.cs
Source:
Enum.cs
Source:
Enum.cs

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

public override string ToString ();

Returns

The string representation of the value of this instance.

Examples

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

Remarks

The return value is formatted with the general format specifier ("G"). 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 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();

See also

Applies to

.NET 9 and other versions
Product Versions
.NET Core 1.0, Core 1.1, Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9
.NET Framework 1.1, 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
.NET Standard 1.0, 1.1, 1.2, 1.3, 1.4, 1.5, 1.6, 2.0, 2.1
UWP 10.0

ToString(String)

Source:
Enum.cs
Source:
Enum.cs
Source:
Enum.cs

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

public string ToString (string format);
public string ToString (string? format);

Parameters

format
String

A format string.

Returns

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

Exceptions

format contains an invalid specification.

format equals "X", but the enumeration type is unknown.

Examples

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

// Sample for Enum.ToString(String)
using System;

class Sample
{
    enum Colors {Red, Green, Blue, Yellow = 12};

    public static void 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
*/

Remarks

The format parameter can be one of the following format strings: "G" or "g", "D" or "d", "X" or "x", and "F" or "f" (the format string is not case-sensitive). If format is null or an empty string (""), the general format specifier ("G") is used. For more information about the enumeration format strings and 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");

See also

Applies to

.NET 9 and other versions
Product Versions
.NET Core 1.0, Core 1.1, Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9
.NET Framework 1.1, 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
.NET Standard 1.0, 1.1, 1.2, 1.3, 1.4, 1.5, 1.6, 2.0, 2.1
UWP 10.0

ToString(IFormatProvider)

Source:
Enum.cs
Source:
Enum.cs
Source:
Enum.cs

Caution

The provider argument is not used. Please use ToString().

Caution

The provider argument is not used. Use ToString() instead.

This method overload is obsolete; use ToString().

[System.Obsolete("The provider argument is not used. Please use ToString().")]
public string ToString (IFormatProvider? provider);
[System.Obsolete("The provider argument is not used. Use ToString() instead.")]
public string ToString (IFormatProvider? provider);
[System.Obsolete("The provider argument is not used. Please use ToString().")]
public string ToString (IFormatProvider provider);
public string ToString (IFormatProvider provider);

Parameters

provider
IFormatProvider

(obsolete)

Returns

The string representation of the value of this instance.

Implements

Attributes

Applies to

.NET 9 and other versions
Product Versions (Obsolete)
.NET (Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9)
.NET Framework 1.1 (2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7 (package-provided), 4.7, 4.7.1 (package-provided), 4.7.1, 4.7.2 (package-provided), 4.7.2, 4.8 (package-provided), 4.8, 4.8.1)
.NET Standard (2.0, 2.1)