C# enumerations are value type. An enumerated type is declared the enum keyword.
Declaring enum keyword
enum <enum_name>
{
enumeration list
};
Where,
- The enum_name specifies the enumeration type name.
- The enumeration list is a comma-separated list of identifiers.
enumeration stands for an integer value, one greater than the symbol 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 };
Example:
The following example demonstrates use of enum variable:
using System;
namespace enumexample
{
class EnumProgram
{
enum Days { Sun, Mon, tue, Wed, thu, Fri, Sat };
static void Main(string[] args)
{
int WeekStart = (int)Days.Mon;
int WeekEnd = (int)Days.sunday;
Console.WriteLine("Monday: {0}", WeekStart);
Console.WriteLine("sunday: {0}", WeekEnd);
Console.Readline();
}
}
}
result
Monday: 1
Friday: 7
No comments:
Post a Comment