Wednesday, 4 January 2012

Enum example in C#

its a collection of similar data types .

we can declare enum as 


enum Enum_name
{
value1 ,     // by default its index is zero 
value2,
value3
}


How can we define start index for enum



enum Enum_name
{
value1 =100,     // by default its index is 100 
value2,
value3
}


How to get enum value 
void main()
{
Enum_name getvalue =Enum_name.value1;
Console.writeLine(getvalue);
}


Loop through Enum Datatype :



using System; 

class MainClass { 
  enum Month { january, feb, mar, apr, may, june,july,aug ,sept,nov,dec }; 

  public static void Main() { 
    Month i;

    for(i = Month .january; i <= Month .dec; i++)  
      Console.WriteLine(i + " has value of " + (int)i); 

  } 
}


No comments:

Post a Comment