C# Tutoriallogin
C# Tutorial
author:php.cn  update time:2022-04-11 14:06:23

C# enumeration (Enum)



An enumeration is a set of named integer constants. Enumeration types are declared using the enum keyword.

C# Enumerations are value data types. In other words, enumerations contain their own values ​​and cannot be inherited or transitively inherited.

Declarationenum Variable

General syntax for declaring an enumeration:

enum <enum_name>
{ 
    enumeration list 
};

Where,

  • enum_name Specifies the type name of the enumeration.

  • enumeration list is a comma-separated list of identifiers.

Each symbol in the enumeration list represents an integer value, an integer value greater than the symbol before it. By default, the value of the first enumeration symbol is 0. For example:

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

Example

The following example demonstrates the use of enumeration variables:

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 When the above code is compiled and executed, it produces the following results:

Monday: 1
Friday: 5

php.cn