Jitesh Byahut 10/4/2015 2381

Enum In C#

An enum or enumeration is a type that consists of a set of named constants.

When we define set of variables list in enum then compiler assigns an integer value to the each members of the enum list starting from 0 and incrementing by one for each successive member in the enum list. This is default behaviour. We can initialize members with your own value to override the default behaviour.

// Define enum - For days of week

enum enumDays { Monday, Tuesday, Wednesday, Thursday, Friday, Saturday, Sunday };

 

// Default integer value of Monday = 0 and therefore

//Tuesday=1, Wednesday=2, Thursday=3, Friday=4, Saturday=5, Sunday=6

By default the enum uses an underlying data type of integer to present the list values. We can change default integer underlying to other data type like: byte, sbyte, short, unshort, uint, long, ulong only. See below examples:

// define byte data type for underlying

enum enumDays : byte { Monday, Tuesday, Wednesday, Thursday, Friday, Saturday, Sunday };

// define short data type for underlying

enum enumDays : short { Monday, Tuesday, Wednesday, Thursday, Friday, Saturday, Sunday };

// define long data type for underlying

enum enumDays : long { Monday, Tuesday, Wednesday, Thursday, Friday, Saturday, Sunday };

Override the default value of enum

We can also define set of enum list with specific values means we can override the default value

    enum enumDays

    {

        Monday = 10,

        Tuesday = 11,

        Wednesday = 12,

        Thursday = 13,

        Friday = 14,

        Saturday = 15,

        Sunday = 16

    };

Get all constant names from enum

enum enumDays { Monday, Tuesday, Wednesday, Thursday, Friday, Saturday, Sunday };

 

static void Main(string[] args)

{

    foreach (string name in Enum.GetNames(typeof(enumDays)))

    {

        Console.Write(name);

    }           

}

 

// Output:

// Monday Tuesday Wednesday Thursday Friday Saturday Sunday

Get all constant values and names from enum

enum enumDays { Monday, Tuesday, Wednesday, Thursday, Friday, Saturday, Sunday };

 

static void Main(string[] args)

{

    foreach (int value in Enum.GetValues(typeof(enumDays)))

    {

        Console.WriteLine(value + " - " + (enumDays)value);

    }

}

 

// Output:

// 0 - Monday

// 1 - Tuesday

// 2 - Wednesday

// 3 - Thursday

// 4 - Friday

// 5 - Saturday

// 6 - Sunday

Get a constant name by value from enum

enum enumDays { Monday, Tuesday, Wednesday, Thursday, Friday, Saturday, Sunday };

 

static void Main(string[] args)

{

    // Get enum constant name which is at index 5

    string dayName = Enum.GetName(typeof(enumDays), 5);

    Console.WriteLine(dayName);               

}

 

// Output:

// Saturday

Get a value by name from enum

enum enumDays { Monday, Tuesday, Wednesday, Thursday, Friday, Saturday, Sunday };

 

static void Main(string[] args)

{

    // Get enum constant value of Friday

    int value = (int)Enum.Parse(typeof(enumDays), "Friday");

    Console.WriteLine(value);               

}

 

// Output:

// 4

Bind dropdownlist from enum in asp.net

var data = from x in Enum.GetNames(typeof(enumDays))

            select new { Text = x, Value = (int)Enum.Parse(typeof(enumDays), x) };

 

DropDownList1.DataSource = data;

DropDownList1.DataValueField = "Value";

DropDownList1.DataTextField = "Text";

DropDownList1.DataBind();


Please give your feedback for improving this page