The best C # enumeration (Enum) Tutorial In 2024, In this tutorial you can learn Enumvariable declaration,Examples,
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.
The general syntax statement enumeration:
enum <enum_name> { enumeration list };
among them,
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};
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