Introduction
The 'enum
' Keyword is of Value Type. It is used to define integral constant and literals. It is inherited from the base class library(BCL) - 'Enum
' where Enum
's static
methods are used by enum
type for parsing enum
values. This article tries to cover some of the enum
functionality through code demonstration. Below is the source code which highlights various enum
methods that are useful during the development phase.
Source Code
using System;
using System.Collections.Generic;
using System.Text;
namespace EnumSample
{
class Program
{
public class EnumExamples
{
public enum Status : byte
{
REPORTED,
OPEN,
VERIFIED,
CLOSED,
REOPEN
}
public enum Colour
{
RED='R',
YELLOW='Y'
}
public enum NumberType
{
WHOLE=0,
NATURAl=1,
EVEN=2,
ODD=3
}
[Flags]
public enum CarOptions
{
SunRoof = 0x01,
Spoiler = 0x02,
FogLights = 0x04,
TintedWindows = 0x08,
}
}
static void Main(string[] args)
{
Console.WriteLine
("Output Enum Byte Value : {0}",(byte)EnumExamples.Status.REPORTED);
Console.WriteLine("Output Enum Character Literal Value :
{0}", (char)EnumExamples.Colour.RED);
Console.WriteLine("Output Enum Integer Value : {0}",
(int)EnumExamples.NumberType.NATURAl);
Console.WriteLine("Output Enum Name as String Datatype : {0}",
EnumExamples.Status.REPORTED.ToString());
Console.WriteLine("Output Enum Name as String Datatype : {0}",
EnumExamples.Colour.RED.ToString());
Console.WriteLine("Output Enum Name as String Datatype : {0}",
EnumExamples.NumberType.NATURAl.ToString());
Console.WriteLine("Output Enum.Parse Return As Typeof(Enum)
With IgnoreCase=False: {0}",
(EnumExamples.Status)Enum.Parse(typeof
(EnumExamples.Status), "REPORTED"));
IgnoreCase=False: REPORTED
Console.WriteLine("Output Enum.Parse Return As Typeof(Enum)
With IgnoreCase=True: {0}",
(EnumExamples.Status)Enum.Parse(typeof
(EnumExamples.Status), "reported", true));
IgnoreCase=True: REPORTED
Console.WriteLine("Output Enum.Parse Return As Typeof(Enum) :
{0}", Enum.GetName(typeof(EnumExamples.Status), 2));
(Literal Constant)**
Console.WriteLine("Output Enum.ToObject Return As Typeof(Enum) :
{0}", Enum.ToObject(typeof(EnumExamples.Colour), 'R'));
Console.WriteLine("Output Enum.Format Return As Typeof(Enum) :
{0}", Enum.Format(typeof(EnumExamples.Colour),
EnumExamples.Colour.RED, "x"));
Console.WriteLine("Output Enum.Format Return As Typeof(Enum) :
{0}", Enum.Format(typeof(EnumExamples.Colour),
EnumExamples.Colour.RED, "d"));
Console.WriteLine("Output Enum.GetUnderlyingType Return Typeof
(Enum) : {0}", Enum.GetUnderlyingType(typeof
(EnumExamples.Colour)));
Console.WriteLine("Output Enum.IsDefined Return as bool indicates
constant in the enum exist or not. : {0}", Enum.IsDefined
(typeof(EnumExamples.Colour), 82));
enum exist or not.:82
int enumCounter = Enum.GetNames(typeof
(EnumExamples.NumberType)).Length;
foreach (string strName in Enum.GetNames(typeof
(EnumExamples.NumberType)))
{
Console.WriteLine("Enum for byte {0}: {1}", Enum.GetNames
(typeof(EnumExamples.NumberType)).Length - enumCounter, strName);
enumCounter--;
}
int enumCounter1 = Enum.GetValues(typeof(EnumExamples.NumberType)).Length;
foreach (int iValues in Enum.GetValues(typeof(EnumExamples.NumberType)))
{
Console.WriteLine("Enum for byte {0}: {1}",
Enum.GetValues
(typeof(EnumExamples.NumberType)).Length - enumCounter1, iValues);
enumCounter1--;
}
EnumExamples.CarOptions options = EnumExamples.CarOptions.SunRoof
| EnumExamples.CarOptions.FogLights;
Console.WriteLine(options);
Console.WriteLine((int)options);
Console.Read();
}
}
}
Feedback
I have tried to incorporate all the possible examples of enum
which can be useful during development. Any suggestions or ideas about the functional usage of 'enum
' will add more value to our learnings.
History
- 16th May, 2008: Initial post