Home  >  Article  >  Backend Development  >  [C# Tutorial] C# Enumeration (Enum)

[C# Tutorial] C# Enumeration (Enum)

黄舟
黄舟Original
2016-12-26 14:37:561329browse

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.

Declare enum variables

General syntax for declaring enumerations:

enum <enum_name>
{ 
    enumeration list 
};

Wherein,

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 usage 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 will produce the following results:

Monday: 1
Friday: 5


The above is the content of [c# tutorial] C# enumeration (Enum), more For more related content, please pay attention to the PHP Chinese website (www.php.cn)!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn