C# Enum ToString() Method

The ToString() method in C# enums converts the enum value to its equivalent string representation. This method provides different formatting options to display enum values as either their names or underlying numeric values.

Syntax

Following are the common syntax forms for enum ToString() method −

enumValue.ToString()          // Returns name
enumValue.ToString("G")       // Returns name (General format)
enumValue.ToString("d")       // Returns decimal value
enumValue.ToString("D")       // Returns decimal value

Parameters

The ToString() method accepts an optional format string parameter −

  • "G" or "g" − General format, returns the enum name (default behavior).

  • "D" or "d" − Decimal format, returns the underlying numeric value.

  • "F" or "f" − Flag format, useful for flag enums with combined values.

Using ToString() with Default Format

Without any format specifier, ToString() returns the enum name −

using System;

public class Demo {
   enum Vehicle { Car, Bus, Truck, Motorbike }

   public static void Main() {
      Vehicle v1 = Vehicle.Car;
      Vehicle v2 = Vehicle.Bus;
      
      Console.WriteLine("Default format:");
      Console.WriteLine("Vehicle.Car = {0}", v1.ToString());
      Console.WriteLine("Vehicle.Bus = {0}", v2.ToString());
   }
}

The output of the above code is −

Default format:
Vehicle.Car = Car
Vehicle.Bus = Bus

Using ToString() with Numeric Format

The "d" or "D" format specifier returns the underlying numeric value of the enum −

using System;

public class Demo {
   enum Vehicle { Car, Bus, Truck, Motorbike }

   public static void Main() {
      Console.WriteLine("Numeric format:");
      Console.WriteLine("Vehicle.Car = {0}", Vehicle.Car.ToString("d"));
      Console.WriteLine("Vehicle.Bus = {0}", Vehicle.Bus.ToString("d"));
      Console.WriteLine("Vehicle.Truck = {0}", Vehicle.Truck.ToString("D"));
      Console.WriteLine("Vehicle.Motorbike = {0}", Vehicle.Motorbike.ToString("D"));
   }
}

The output of the above code is −

Numeric format:
Vehicle.Car = 0
Vehicle.Bus = 1
Vehicle.Truck = 2
Vehicle.Motorbike = 3

Using ToString() with Custom Enum Values

When enums have custom numeric values, ToString() still works correctly −

using System;

public class Demo {
   enum StatusCode { Success = 200, NotFound = 404, ServerError = 500 }

   public static void Main() {
      Console.WriteLine("Custom enum values:");
      Console.WriteLine("Name: {0}, Value: {1}", StatusCode.Success.ToString(), StatusCode.Success.ToString("d"));
      Console.WriteLine("Name: {0}, Value: {1}", StatusCode.NotFound.ToString(), StatusCode.NotFound.ToString("d"));
      Console.WriteLine("Name: {0}, Value: {1}", StatusCode.ServerError.ToString(), StatusCode.ServerError.ToString("d"));
   }
}

The output of the above code is −

Custom enum values:
Name: Success, Value: 200
Name: NotFound, Value: 404
Name: ServerError, Value: 500

Comparison of Format Options

Format Specifier Description Example Output
None (default) Returns enum name Car
"G" or "g" General format (same as default) Car
"D" or "d" Decimal value 0
"F" or "f" Flag format (for flag enums) Car

Conclusion

The enum ToString() method in C# provides flexible formatting options to convert enum values to string representations. Use the default format to get enum names, or specify "d"/"D" to get the underlying numeric values, making it useful for debugging and display purposes.

Updated on: 2026-03-17T07:04:35+05:30

3K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements