Loading...

Enumerators or Enum in C#, Easy Tutorial

What are Enumerators in C#:

First of all we have to know that what are enums, so the very simple answer is Enums are the User Defined Data Types. It means that it is a distinct Data Type which user defines, and not a predefined data type given by the C#. Enumerators in C#have numbers associated with the values. 
By default, the first element of enum is assigned a value of 0 and is increment for each subsequent enum element. The default value can be overridden during initialization.


  public enum food { pizza = 1, burger = 2 };


Example:

class Clock
    {
        public enum TYPE { HOURS12, HOURS24 } // now hour12 and hour24 will act as normal variable like int a where a is variable.

        public enum AMPM { AM, PM }
        public TYPE type;
        private AMPM ampm;

public Clock()
        {
            type = TYPE.HOURS12;  // how to to use enum variable  
            ampm = AMPM.AM;
            hours = 1; minutes = 0; seconds = 0;
        }
    }

Hit Like Button if you understood, It helps a lot to keep us Motivated.

0 comments:

Post a Comment

Google+

 
TOP