C# Enum ToString() Method

CsharpProgrammingServer Side Programming

The ToString() method converts the value of this instance to its equivalent string representation.

Firstly, set an enum.

enum Vehicle { Car, Bus, Truck, Motobike };

To convert it to an equivalent string representation, use ToString().

Vehicle.Car.ToString("d")

Example

 Live Demo

using System;
public class Demo {
   enum Vehicle { Car, Bus, Truck, Motobike };
   public static void Main() {
      Console.WriteLine("Vehicle.Car = {0}", Vehicle.Car.ToString("d"));
      Console.WriteLine("Vehicle.Bus = {0}", Vehicle.Bus.ToString("d"));
   }
}

Output

Vehicle.Car = 0
Vehicle.Bus = 1
raja
Published on 12-Sep-2018 13:05:42
Advertisements