Sunday, July 6, 2014

ENUM

What is enum
It is the value type with set of related named constants
Often referred to as an enumerator list
Enum type can be integer, float.but if you use beside int it has to be cast

Enum is used to create numeric constants in .net framework
By default, the first enumerator has the value 0 , and the value of each successive enumerator is increased by 1
 Enum dow    {sat,sun,mon,tue,wed,thu,fri };

namespaceConsoleApplication2
{
    class program
    {
        public enum DayofWeek
        {
            sunday=1,monday=2,tuesday=3,wednesday=4,thursday,friday,saturday
        }
        static void Main(string[] args)
        {
            Console.WriteLine("Day of week {0} {1}", (int)DayofWeek.sunday, DayofWeek.sunday);
            Console.WriteLine("Day of week {0} {1}", (int)DayofWeek.monday, DayofWeek.monday);
            Console.WriteLine("Day of week {0} {1}", (int)DayofWeek.tuesday, DayofWeek.tuesday);
            Console.WriteLine("Day of week {0} {1}", (int)DayofWeek.wednesday, DayofWeek.wednesday);
            Console.ReadKey();
        }
    }
}

Output:





some important points about enum
enum are enumerated data types in c sharp

enum are strongly typed constants they are  strongly type i,e  an enum of one type may not be implicit assigned to an enum of another type.

Enum values are fixed , enum can be displayed as string and processed as an integer.

Enum are value type and are created on stack and not on heap
               
    class program
    {
        publicenum Volume
        {
            low, medium, high
        }
        classEnumSwitch
        {
            staticvoid Main()
            {
                Volume myvolume = Volume.medium;
                switch (myvolume)
                {
                    case Volume.low:
                        Console.WriteLine("the volume is low");
                        break;
                    case Volume.medium:
                        Console.WriteLine("the volume is medium");
                        break;
                    case Volume.high:
                        Console.WriteLine("the volume is high");
                        break;
                }
                Console.ReadKey();
            }
        }
    }

No comments:

Post a Comment

Thank you for visiting my blog

Kubernetes

Prerequisites We assume anyone who wants to understand Kubernetes should have an understating of how the Docker works, how the Docker images...