[C# Tutorial] C# Enumeration (Enum)

黄舟
Release: 2016-12-26 14:37:56
Original
1370 people have browsed it

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 
};
Copy after login

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 };
Copy after login

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

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

Monday: 1
Friday: 5
Copy after login


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)!

Related labels:
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template