Wednesday, April 4, 2012

Enumeration in C#


An enumeration type (also named an enumeration or an enum) provides an efficient way to define a set of named integral constants that may be assigned to a variable.
An enumeration type is a distinct type with named constants. Every enumeration type has an underlying type, which can be either byte, short, int, or long. Enumeration types are defined through enumeration declarations An enum type declaration defines a type name for a related group of symbolic constants. The body of an enum type declaration defines zero or more enum members, which are the named constants of the enum type. No two enum members can have the same name. An enum declaration can not contain declarations of methods, properties, events, operators, or types.
The associated value of an enum member is assigned either implicitly or explicitly. If the declaration of the enum member has a constant-expression initializer, the value of that constant expression, implicitly converted to the underlying type of the enum, is the associated value of the enum member.
If the declaration of the enum member has no initializer, its associated value is set implicitly, as follows:
· If the enum member is the first enum member declared in the enum type, its associated value is zero.
· Otherwise, the associated value of the enum member is obtained by increasing the associated value of the previous enum member by one. This increased value must be within the range of values that can be represented by the underlying type.

Syntax : enum <enumname>
         {
            constStringname1=<value>,
            constStringname2=<value>,
          }

For better Understand let's write a sample application :


//defining the enum
    enum Color
    {
        Red,
        Green = 10,
        Blue
    }

    class Enumex
    {
        static void Main()
        {
            Console.WriteLine(StringFromColor(Color.Red));
            Console.WriteLine(StringFromColor(Color.Green));
            Console.WriteLine(StringFromColor(Color.Blue));
            Console.ReadLine();
        }
        static string StringFromColor(Color c)
        {
            switch (c)
            {
                case Color.Red:
                    return String.Format("Red = {0}", (int)c);
                case Color.Green:
                    return String.Format("Green = {0}", (int)c);
                case Color.Blue:
                    return String.Format("Blue = {0}", (int)c);
                default:
                    return "Invalid color";
            }
        }

    }


prints out the enum member names and their associated values.

output:
 



For the following reasons you got this output :
· the enum member Red is automatically assigned the value zero (since it has no initializer and is the first enum member);
· the enum member Blue is explicitly given the value 10;
· and the enum member Green is automatically assigned the value one greater than the member that textually precedes it.



No comments:

Post a Comment