Enum.GetName in C#


Gets the string representation of an Enum value using the Enum.GetName.

It has two parameters −

  • Type − Enumeration type

  • Object − Value of an enumeration

The following is an example −

Example

 Live Demo

using System;

class Demo {
   enum Vehicle {
      Car,
      Motorbike,
      Truck,
      Bicycles
   };
   static void Main() {
      // Usig GetName to get the string representation of enum type
      string res = Enum.GetName(typeof(Vehicle), Vehicle.Motorbike);
   
      // Displaying
      Console.WriteLine(res);
   }
}

Output

Motorbike

Updated on: 22-Jun-2020

525 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements