SIGN UP MEMBER LOGIN:    
ARTICLE

Facts of Enumerations in C#

Posted by Abhimanyu Kumar Vatsa Articles | C# Language July 06, 2011
In this quick article you will take a look at the facts of Enumerations in C#.
Reader Level:

Introduction 

You must be aware of enumerations (or simply enum), but there are many things we should always keep in mind about them. Let's begin with the basics. 

C# has two fundamental types, value types and reference types. A value type variable holds values directly on the stack whereas a reference type variable holds a reference to a variable or object on the heap. 

In value types, C# has enumerations and structures. 

Why we use Enumerations? 

Okay, suppose we want to work with name of the day of the week. We could use the integers 0, 1, 2 and so on to represent Sunday, Monday, Tuesday, etc. respectively. This system will work but it is not very intuitive, or say it is not a robust solution because if we use the integer value 0 in code, it would not be obvious that a particular 0 represents Sunday. One more thing is if we declare an integer variable named day, then there is nothing preventing someone from assigning any legal integer value apart from 0, 1, ...., 5, 6. Here C# offers much better experience for us by using enumerations (enum) where the weekday's values are limited to a defined set. An enumeration type provides an efficient way to define a set of named integral constants that may be assigned to a variable. For example, assume that we have to define a variable whose value will represent a day of the week. There are only seven meaningful values which that variable will ever store. To define those values, we can use an enumeration type, which is declared by using the enum keyword. 

How to use Enumerations? 

The enum keyword is used to declare an enumeration consisting of a set of named constants called the enumerator list. Every enumeration type has an underlying type, which can be any integral type (byte, sbyte, short, ushort, int, uint, long, or ulong) except char. The default underlying type of the enumeration elements is int. By default, the first enumerator has the value 0, and the value of each successive enumerator is increased by 1. 

For example: 

enum Days {Sunday, Monday, Tuesday, Wednesday, Thursday, Friday, Saturday}; 

Let's use the above example in a program to see at its integral type factor. 

using System;
public
 class EnumTest
{
    enum Days { Sunday, Monday, Tuesday, Wednesday, Thursday, Friday, Saturday };
    static void Main()
    {
        int x = (int)Days.Sunday;
        int y = (int)Days.Saturday;
        Console.WriteLine("Sunday = {0}", x);
        Console.WriteLine("Saturday = {0}", y);
        Console.ReadKey();
    }
}
 

Output:

Sunday = 0

Saturday = 6 

In the above program, I'm casting (even it is important to cast always) the Days.Sunday and Days.Saturday to integer type and assigning it to new x and y integer variables. 

Look at another example where, Sunday begins with 2, then Monday will be 3, Tuesday will be 4,…, and Saturday will be 8. Enumerators can have initializes to override the default values. 

For example: 

using System;
public
 class EnumTest
{
    enum Days { Sunday=2, Monday, Tuesday, Wednesday, Thursday, Friday, Saturday };
    static void Main()
    {
        int x = (int)Days.Sunday;
        int y = (int)Days.Saturday;
        Console.WriteLine("Sunday = {0}", x);
        Console.WriteLine("Saturday = {0}", y);
        Console.ReadKey();
    }
}
 

Output:

Sunday = 2

Saturday = 8 

Look at one more example, which has a base-type option, used to declare an enum whose members are of the type long. Notice that even though the underlying type of the enumeration is long, the enumeration members must still be explicitly converted to type long using a cast. 

For example: 

using System;
public
 class EnumTest
{
    enum Range : long { Num1 = 43567883635L, Num2 = 310L };
    public static void Main()
    {
        long x = (long)Range.Num1;
        long y = (long)Range.Num2;
        Console.WriteLine("Num1 = {0}", x);
        Console.WriteLine("Num2 = {0}", y);
        Console.ReadKey();
    }
}
 

Output:

Num1 = 43567883635L

Num2 = 310L 

We can print an all enum list as in the following example: 

using System;
public
 class EnumTest
{
    enum Days { Sunday, Monday, Tuesday, Wednesday, Thursday, Friday, Saturday };
    static void Main()
    {
        foreach (int i in Enum.GetValues(typeof(Days)))
        Console.WriteLine(i);
        Console.ReadKey();
    }
}
 

Output:

0

1

2

3

4

5

6 

That's all about the enumerations. Thanks for reading.

Login to add your contents and source code to this article
share this article :
post comment
 

I like this topic description and examples which is very easily understand

Posted by vandana walsange Jul 10, 2011

Thanks abhi, your articles are very useful for beginners.

Posted by Dinesh Beniwal Jul 07, 2011
Nevron Gauge for SharePoint
Become a Sponsor
PREMIUM SPONSORS
  • ceTE software specializes in components for dynamic PDF generation and manipulation. The DynamicPDF™ product line allows you to dynamically generate PDF documents, merge PDF documents and new content to existing PDF documents from within your applications. Visit DynamicPDF here
    Finally – a virtual platform that delivers next-generation Windows Server 2008 Hyper-V virtualization technology from a managed hosting partner you can truly depend on. Visit www.maximumasp.com/max for a FREE 30 day trial. Hurry offer ends soon. Climb aboard the MaxV platform and take advantage of High Availability, Intelligent Monitoring, Recurrent Backups, and Scalability – with no hassle or hidden fees. As a managed hosting partner focused solely on Microsoft technologies since 2000, MaximumASP is uniquely qualified to provide the superior support that our business is built on. Unparalleled expertise with Microsoft technologies lead to working directly with Microsoft as first to offer IIS 7 and SQL 2008 betas in a hosted environment; partnering in the Go Live Program for Hyper-V; and product co-launches built on WS 2008 with Hyper-V technology.
Team Foundation Server Hosting
Become a Sponsor