How do we use enum keyword to define a variable type in C#?


C# enumerations are value data type. An enumeration is a set of named integer constants. An enumerated type is declared using the enum keyword.

The following is the syntax of enum.

enum <enum_name> {
   enumeration list
};

Let us see an example.

enum Vehicle { Car, Bus, Truck };

The following is an example showing how to use enum keyword to define variable type.

Example

 Live Demo

using System;
namespace Demo {
   class Program {
      enum Days { Sun, Mon, tue, Wed, thu, Fri, Sat };
      static void Main(string[] args) {
         int WeekdayStart = (int)Days.Mon;
         int WeekdayEnd = (int)Days.Fri;
         Console.WriteLine("Monday: {0}", WeekdayStart);
         Console.WriteLine("Friday: {0}", WeekdayEnd);
         Console.ReadKey();
      }
   }
}

Output

Monday: 1
Friday: 5

karthikeya Boyini
karthikeya Boyini

I love programming (: That's all I know

Updated on: 23-Jun-2020

61 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements