Skip to main content

How to Get the Name of an Enum Value with ToString in C#

· 6 min read
Jagdish Kumawat
Founder @ Dewiride

heading.ToString() returns the name of the member that heading holds, such as "East", as an ordinary string. Putting the value in an interpolated string, as in $"Turn {heading}", writes the same name.

Microsoft's API reference on Enum.ToString(): Converts the value of this instance to its equivalent string representation. For an ordinary enum, that's the member's name: 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.

Syntax​

Syntax
string <name> = <value>.ToString();
string <number> = <value>.ToString("D");
string <text> = $"... {<value>} ...";
string <text> = $"... {<value>:D} ...";
PartWhat it is
<value>.ToString()The member's name, such as "East"
<value>.ToString("D")The number behind the member, as text, such as "1"
{<value>} in an interpolated stringWrites the member's name
{<value>:D} in an interpolated stringWrites the number

The format strings an enum accepts: "G" or "g", "D" or "d", "X" or "x", and "F" or "f" (the format string is not case-sensitive). With no format, ToString() uses G, which gives the name.

The Example​

A console app called EnumName has a Direction enum with four compass points. It turns Direction.East into text in three ways, and prints a value that has no member.

Prerequisites​

  • The .NET 10 SDK. dotnet --version prints a version that starts with 10.0.

Step 1: Create a Console App​

Terminal
dotnet new console -n EnumName
cd EnumName
Output
The template "Console App" was created successfully.

Processing post-creation actions...
Restoring /Users/you/source/EnumName/EnumName.csproj:
Determining projects to restore...
Restored /Users/you/source/EnumName/EnumName.csproj (in 40 ms).
Restore succeeded.

dotnet new console -n EnumName creates a folder named EnumName with a project file and a Program.cs, and cd moves into it. Everything this example uses is part of .NET, so there's no package to add. Open the folder in VS Code, or EnumName.csproj in Visual Studio.

Step 2: Write the Code​

Replace everything in Program.cs. First the name, as a string and in a message:

Program.cs (the name)
Direction heading = Direction.East;

// ToString gives the member's name as a string.
string name = heading.ToString();
Console.WriteLine($"Name: {name} ({name.Length} letters)");

// String interpolation writes the same name.
Console.WriteLine($"Turn {heading}");
  • heading.ToString() returns the member's name as an ordinary string, so name.Length works on it like on any other string.
  • $"Turn {heading}" puts the value in an interpolated string. The C# reference: the compiler replaces items with interpolation expressions by the string representations of the expression results.

Then the number, and a value without a member:

Program.cs (the number)
// The D format gives the number instead of the name.
Console.WriteLine($"Number: {heading:D}");

// A value with no member prints as its number.
Direction unknown = (Direction)7;
Console.WriteLine($"No member: {unknown}");
  • {heading:D} adds the format string D after a colon. The format strings reference: Displays the enumeration entry as an integer value in the shortest representation possible.
  • (Direction)7 casts a number that no member has.

And the enum, at the end of the file:

Program.cs (the enum)
enum Direction
{
North,
East,
South,
West,
}

No member has a number written next to it, so North is 0, East 1, South 2 and West 3.

Step 3: Run It​

Run it in VS Code's terminal, or press Ctrl+F5 in Visual Studio:

Terminal
dotnet run
Output
Name: East (4 letters)
Turn East
Number: 1
No member: 7

macOS-style terminal window titled Terminal — EnumName showing dotnet run printing Name: East (4 letters), Turn East, Number: 1 and No member: 7

ToString() gave East, an ordinary string of 4 letters, and the interpolated string wrote the same name. The D format gave 1, the number behind East. (Direction)7 has no name to give, so it printed 7. The reference: Otherwise, the return value is the string representation of the numeric value of this instance.

The Complete File​

macOS-style code window titled Program.cs — EnumName showing a Direction variable set to Direction.East, its ToString result stored in name and printed with its length, the value in an interpolated string, the value with the D format, a Direction cast from 7, and the enum Direction with North, East, South and West

Program.cs
Direction heading = Direction.East;

// ToString gives the member's name as a string.
string name = heading.ToString();
Console.WriteLine($"Name: {name} ({name.Length} letters)");

// String interpolation writes the same name.
Console.WriteLine($"Turn {heading}");

// The D format gives the number instead of the name.
Console.WriteLine($"Number: {heading:D}");

// A value with no member prints as its number.
Direction unknown = (Direction)7;
Console.WriteLine($"No member: {unknown}");

enum Direction
{
North,
East,
South,
West,
}

Common Mistakes​

MistakeWhat happensDo this instead
Using nameof on the variable, as in nameof(heading)It printed heading, the variable's name, not EastCall heading.ToString(). The reference: A nameof expression is evaluated at compile time and has no effect at run time, so it never sees the value. nameof(Direction.East) does print East, because it names the member itself
Using a number format, such as heading.ToString("N0")System.FormatException: Format string can be only "G", "g", "X", "x", "F", "f", "D" or "d".Use D for the number
Giving two members the same number, as in Gray = 1, Grey = 1A Shade set to Shade.Grey printed GrayGive each member its own number. The reference: your code should not make any assumptions about which name the method will return.

FAQ​

What does Enum.GetName return?​

The same name, or null when the value has no member. Enum.GetName(heading) returned East, and Enum.GetName((Direction)7) returned null where ToString() gave 7.

Do I have to call ToString when I build a string?​

No. Interpolation writes the name for you, and so does +: "Heading " + heading printed Heading East.

How does a Flags enum print?​

As a list of names. With [Flags] on the enum, Permission.Read | Permission.Write printed Read, Write, and the D format gave 3.

Conclusion​

Call ToString() on an enum value to get its member's name as a string, or put the value straight into an interpolated string. Add :D, or call ToString("D"), when you want the number instead. A value without a member gives its number, not a name.

Additional Resources​