Enum Data Type in C sharp

Posted: February 20, 2013 in Object Oriented concept Via C#
Tags: , ,

Hello ,

This article is about C # data type ENUM.

EXAMPLE 1:

enum WeekDays
{
   Sunday,
   Monday,      //ENUM DECLARATION 
   Tuesday,
   Wednesday,
   Thursday,
   Friday,
   Saturday
}

class MyProgram
{
  public static void Main()
  {
      System.Console.WriteLine(WeekDays.Sunday);
      System.Console.WriteLine(WeekDays.Monday);      
      System.Console.WriteLine(WeekDays.Tuesday);      
      System.Console.WriteLine(WeekDays.Wednesday);
      System.Console.WriteLine(WeekDays.Thursday);
      System.Console.WriteLine(WeekDays.Friday);      
      System.Console.WriteLine(WeekDays.Saturday);      
  }
}

OUTPUT:
Sunday
Monday
Tuesday
Wednesday
Thursday
Friday
Saturday

Explanation :Enum allows us to create a new data type. In the above program 
you get to see a data type WeekDays created using enum. An enum can be considered
 like a static object of a class. This is the reason why we are allowed to access
 the elements within enum by using just the name of the enum WeekDays, like WeekDays.Sunday,
 where WeekDays is the name of the enum and Sunday is an element of the enum WeekDays. 
In the WriteLine() method when we pass the element within the enum as enumName.elementName
 (i.e. WeekDays.Sunday)the output returned is the element name (i.e Sunday in our case). 
This is justified by the output that you get to see for the above program.

EXAMPLE :2 now if we want to print numerical numbers, then
enum WeekDays
{
   Sunday,
   Monday, 
   Tuesday,
   Wednesday,
   Thursday,
   Friday,
   Saturday
}

class MyProgram
{
  public static void Main()
  {
      System.Console.WriteLine((int)WeekDays.Sunday);
      System.Console.WriteLine((int)WeekDays.Monday);      // TYPE CASTING OF weekdays.dayname
      System.Console.WriteLine((int)WeekDays.Tuesday);         to integer type.
      System.Console.WriteLine((int)WeekDays.Wednesday);
      System.Console.WriteLine((int)WeekDays.Thursday);
      System.Console.WriteLine((int)WeekDays.Friday);      
      System.Console.WriteLine((int)WeekDays.Saturday);      
  }
}
OUTPUT:
0
1
2
3
4
5
6

By default, enum numeral start from 0.

EXAMPLE 3: an enum can not inherited another enum. 
The succeeding elements get a value in incrementing steps of 1. 
Thus, Monday gets a value of 1, Tuesday gets a value of 2,
 Wednesday gets a value of 3 and so on and so forth.
enum InitialDays
{
   Sunday,
   Monday, 
   Tuesday,
   Wednesday,
}

enum WeekDays : InitialDays
{
   Thursday,
   Friday,
   Saturday
}

class MyProgarm
{
  public static void Main()
  {
      System.Console.WriteLine((int)WeekDays.Sunday);
      System.Console.WriteLine((int)WeekDays.Monday);      
      System.Console.WriteLine((int)WeekDays.Tuesday);      
      System.Console.WriteLine((int)WeekDays.Wednesday);
      System.Console.WriteLine((int)WeekDays.Thursday);
      System.Console.WriteLine((int)WeekDays.Friday);      
      System.Console.WriteLine((int)WeekDays.Saturday);      
  }
}

OUTPUT: Error will occur , Think Think !!!

Explanation: Error! Enums cannot be derived from other enums.
 Hence we are disallowed to derive enum WeekDays from InitialDays.

Example4:
enum WeekDays
{
   Sunday,
   Monday, 
   Tuesday,
   Wednesday,
   Thursday,
   Friday,
   Saturday
}

class MyProgram : WeekDays
{
  public static void Main()
  {
      System.Console.WriteLine((int)WeekDays.Sunday);
      System.Console.WriteLine((int)WeekDays.Monday);      
      System.Console.WriteLine((int)WeekDays.Tuesday);      
      System.Console.WriteLine((int)WeekDays.Wednesday);
      System.Console.WriteLine((int)WeekDays.Thursday);
      System.Console.WriteLine((int)WeekDays.Friday);      
      System.Console.WriteLine((int)WeekDays.Saturday);      
  }
}
OUTPUT : ERROR !! Error !! Think why ??
Explantion:Enum are treated as sealed class, so class can not inherited the enum weekdays.
Note:"like classes,Enum cannot contain two members having the same name."

Example:
enum WeekDays
{
   Sunday,
   Monday, 
   Tuesday,
   Wednesday,
   Thursday,
   Friday,
   Saturday
}

class MyProgram
{
  public static void Main()
  {
     System.Console.WriteLine(WeekDays.Wednesday.CompareTo(WeekDays.Sunday));
     System.Console.WriteLine(WeekDays.Wednesday.CompareTo(
         WeekDays.Wednesday));
     System.Console.WriteLine(WeekDays.Wednesday.CompareTo(
         WeekDays.Saturday));
  }
}

OUTPUT:
1
0
-1
Explanation: What if you had to compare members within enums?
 One may want to know if the enum member larger,
 smaller or equal to another enum member under consideration.
 To accomplish this we have made use of a method named CompareTo(). 
This method belongs to the Enum class. An enum is derived from the Enum class.
 Since Wednesday is greater than Sunday in our enum WeekDays, the output is 1.
 Similarly, since Wednesday is equal to Wednesday the output is 0.
 Likewise the output is -1 because Wednesday is less than Saturday.

So that is all about Enum, what it will do for your appliacation?? Think 

Till then !! Happy coding
Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s