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);

 




HTML Comment Box is loading comments...



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.