Sponsored Ad

Wednesday, May 12, 2010

Enum Example in C# | How Enum Works

 

Enums are used to create a new data type. It is basically use to give a user friendly name to an integer value.

Like in given example

enum eTest

{

a1 = 4, a2, a3 = 9, a4

}

We have created a enum with name eTest and it have 4 variable which are associated with different integer values.

A1=4

A2=5

A3=9

A4=10

Enum by default start with 0 and keep incrementing 1 value for next variables.

 

Output:

Enum exampe output

 

Source Code:

using System;
using System.Collections.Generic;
using System.Text;

namespace ConsoleApplication1
{
    class Program
    {
        enum eTest
        {
            a1 = 4, a2, a3 = 9, a4
        }

        static void Main(string[] args)
        {
            Program obj = new Program();
            obj.EnumExample();
            Console.ReadLine();
        }

        private void EnumExample()
        {
            Console.WriteLine((int)eTest.a1 + " " + (int)eTest.a2 + " " + (int)eTest.a3 + " " + (int)eTest.a4);
        }
    }
}

No comments:

Post a Comment

Sponsored Ad

Development Updates