C# tutorial-enum

C# enum

An enum is a collection of named constants called emumerator list. The type of an enum must be integral type (byte, sbyte, short, ushort, int, uint, long, ulong). To declare an enum, you can use enum keyword followed by its name


 enum WeekDays{Mon, Tue, Wed, Thu,  Fri, Sat, Sun};

You can also specify the type the enum by placing the type after the semi-coln.


enum WeekDays : int {Mon, Tue, Wed, Thu, Fri, Sat, Sun};

By default, the elements of the enum store values starting from 0 upward(Mon=0, Tue=1,…,Sun=6).

If you do not want to accept the default values, you can change them as you like. In the case below, the values start from 1 instead of 0.

            enum WeekDays{Mon=1, Tue, Wed, Thu,  Fri, Sat, Sun};


To access the values of the enum WeekDays, you can do as the following:

            int x=(int)WeekDays.Mon;




Ly comment


Generally, enum object is defined in the class level so it is known to all methods in the class. You no need to pass the enum to any method. However, you can try to pass the enum by creating a method that returns the enum. Then create object of the class and pass the object to your method. After that you can access the enum within that method.

2013-10-01 20:34:00
Liz Ravenwood comment

 Liz Ravenwood

It would be helpful to know how to pass them from one function to another. I'm clueless.

2013-09-30 15:08:00

This website intents to provide free and high quality tutorials, examples, exercises and solutions, questions and answers of programming and scripting languages:
C, C++, C#, Java, VB.NET, Python, VBA,PHP & Mysql, SQL, JSP, ASP.NET,HTML, CSS, JQuery, JavaScript and other applications such as MS Excel, MS Access, and MS Word. However, we don't guarantee all things of the web are accurate. If you find any error, please report it then we will take actions to correct it as soon as possible.