Enum
enum is an keyword to inform compiler that it is an enumeration.
Enumeration consist a set of values related name constant
, it is called enumeration list.The value should be distinct. Enumeration is
declared generally under a namespace, but it can be declared under
structure and class also. It is a value type object and do not support
inheritance. By default for the set of value , enumeration value start
from zero if not specified. Enumeration value can be integer, byte, float , double.Below is the
enumeration with default enumeration value.
enumeration with default enumeration value.
Example 1
using System;
namespace
student
{
public
class
Student
{
enum
DayofWeek
{
Sunday,
Monday, Tuesday, Wednesday, Thrusdad, Friday, Saturday
};
static
void
Main()
{
int
i = (int)DayofWeek.Tuesday;
Console.WriteLine("Value
of i is "
+ i.ToString());
Console.ReadKey();
}
}
Output :
Value of i is 2
Example 2
Below is the example with first enumeration value 12
Example 2
Below is the example with first enumeration value 12
using
System;
namespace
student
{
public
class
Student
{
enum
DayofWeek
{
Sunday=12,
Monday, Tuesday, Wednesday, Thrusdad, Friday, Saturday
};
static
void
Main()
{
int
i = (int)DayofWeek.Tuesday;
Console.WriteLine("Value
of i is "
+ i.ToString());
Console.ReadKey();
}
}
}
No comments:
Post a Comment