Latest web development tutorials

C # enumeration (Enum)

Enumeration is a set of named integer constants. Enumerated type is declared usingenum keyword.

C # is a value enumeration data type. In other words, the enumeration contains its own value and can not be inherited or transfer inheritance.

Enumvariable declaration

The general syntax statement enumeration:

enum <enum_name>
{ 
    enumeration list 
};

among them,

  • enum_namespecified type name enumeration.
  • enumeration listis a comma-separated list of identifiers.

Enumerated list Each symbol represents an integer value larger than a signed integer value that precedes it. By default, the value of the first enumeration symbol is 0. For example:

enum Days {Sun, Mon, tue, Wed, thu, Fri, Sat};

Examples

The following example demonstrates the use of enumeration:

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 ();
      }
   }
}

When the above code is compiled and executed, it produces the following results:

Monday: 1
Friday: 5