C#-Enums

Enums in C#

The C#-Enums keyword is used to declare an enumeration: a type that consists of a set of named constants called the enumerator list.

By default, the first enumerator has the value 0, and the value of each successive enumerator is increased by 1.


C#-Enums Syntax :

enum <enum_name> {
   enumeration list 
};

Where,

1.The <i>enum_name</i> specifies the enumeration type name.

2. The <i>enumeration list</i> is a comma-separated list of identifiers.


Example :

The following example demonstrates use of enum variable :

using System;

namespace EnumApplication {
   class EnumProgram {
      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();
      }
   }
}


Recommended Posts:

Spread the love

Leave a Comment

Your email address will not be published. Required fields are marked *