C # enumeration


Release date:2023-08-30 Update date:2023-10-13 Editor:admin View counts:332

Label:

C # enumeration

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

The C # enumeration is a value type. In other words, enumerations contain their own values and cannot inherit or pass inheritance.

Statement enum variable

General syntax for declaring enumerations:

enum <enum_name>
{
    enumeration list
};

Among them

  • enum_name specifies the type name of the enumeration.

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

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

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

Example

The following example demonstrates the use of enumerated variables:

Example

using System;
public class EnumTest
{
    enum Day { Sun, Mon, Tue, Wed, Thu, Fri, Sat };
    static void Main()
    {
        int x = (int)Day.Sun;
        int y = (int)Day.Fri;
        Console.WriteLine("Sun = {0}", x);
        Console.WriteLine("Fri = {0}", y);
    }
}

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

Sun = 0
Fri = 5

Powered by TorCMS (https://github.com/bukun/TorCMS).