ENUMERATION
The verbal meaning of enumeration is nothing but "counting". The enum keyword in C# is used to declare an enumeration. The default type of enumeration is int. The default value of the first element is "0". For every successor the value "1" will be incremented.
The syntax to declare an enum is:
[attributes] [modifiers] enum identifier [:base-type] {enumerator-list} [;]
attributes -Optional
modifiers -Optional
identifier - enum
base-type - Optional
The default is int. Can't use char.
enumerator-list
Example 1:
using System;
class Program
{
enum Days { a = 5, b, c, d, e, f };
public static void Main()
{
long x = (long)Days.a;
long y = (long)Days.c;
Console.WriteLine(x.ToString());
Console.WriteLine(y.ToString());
}
}
Output
2
7
Here the declared value of a is 5; if not declared then the default value will be 0. For each variable the value is increment by 1.
a=5
b=6
c=7
d=8
e=9
f=10
Example 2:
using System;
class Program
{
enum Days { a = 5, b, c = 10, d, e, f };
public static void Main()
{
long x = (long)Days.a;
long y = (long)Days.d;
Console.WriteLine(x.ToString());
Console.WriteLine(y.ToString());
}
}
Output
5
11
The value of other variables
a=5
b=6
c=10
d=11
e=12
f=13
Example 3
using System;
class Program
{
enum Range : long { max = 100, min = 10 }
public static void Main()
{
long x = (long)Range.max;
long y = (long)Range.min;
Console.WriteLine(x.ToString());
Console.WriteLine(y.ToString());
}
}