Enum Keyword:
The Enum also called ad Enumeration. This is the
efficient way to declare Integral constants
The enum key word is used to declare Enumeration, a distinct type that consists of a set of named constants called
the enumerator list in C#.
Usually it is best to define an
enum directly within a namespace so that all classes in the namespace can
access it with equal convenience. However, an enum can also be nested within a
class or struct.
Every Enumeration has integral
type, which can be either int, long, short or byte. The default underlying type of the
enumeration element is Int and default value is 0.
The
Value of enum is assigned either implicitly or explicitly.
If the
declaration of enum has initialization with any constant value, that value is
implicitly converted into enum type. If the declaration doesn’t have any
initialization, it takes a value implicitly as follows
è
if it is the first member in the enum list,
then it is initialized with zero ‘0’
è
if it is not first member in the enum list,
then its value is obtained by incrementing it previous value.
Syntax:
Enum <Enumname>
{
Member1 = value1;
Member2 = value2
..
}
Ex:
enum Days {Sat, Sun, Mon, Tue, Wed, Thu, Fri};
Example program:
using System;
using
System.Collections.Generic;
using
System.Text;
namespace Enum1
{
public enum Days { Mon,
tue, wed, thur=10, fri, sat, sun };
class Program
{
static void Main (string[] args)
{
Console.WriteLine("Monday {0} ", (int)Days.Mon);
Console.WriteLine("Tuesday {0} ",(int)Days.tue);
Console.WriteLine("Wedness Day {0}",(int) Days.wed);
Console.WriteLine("Thursday {0} ", (int)Days.thur);
Console.WriteLine("Friday {0} ", (int)Days.fri);
Console.WriteLine("saturday {0}
",(int) Days.sat);
Console.ReadKey();
}
}
}
Output:
No comments:
Post a Comment