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

Example:

 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.

Example:

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;

            Console.WriteLine("Mon={0}",x);

 





Comments

Ly comment

 Ly

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.

10-01-2013
Liz Ravenwood comment

 Liz Ravenwood

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

09-30-2013
Name:
Comment:
Enter the text:
CAPTCHA image

....................................................................................................................Home | Forum | About | Contact
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.
Copyright @ 2011-2013 worldbestlearningcenter. All Rights Reserved.
Home Programming Web DB & App Questions Exercises Tips Programs FAQs Download About
Computer-Wbest
Popular Pages
You might like