by Dinesh Thakur Category: Introduction to C#

An enumeration is a special set of numeric type string literals which considered as a constants.

An enumeration is a special kind of value type limited to a restricted and unchangeable set of numerical values. By default, these numerical values are integers, but they can also be longs, bytes, etc. (any numerical/value except char) as will be illustrated below.

When you define an enumeration you provide literals which are then used as constants for their corresponding values. The following code shows an example of such a definition:

 

public enum DAYS

{

Monday,

Tuesday,

Wednesday,

Thursday,

Friday,

Saturday,

Sunday

}

 

Note, however, that there are no numerical values specified in the above. Instead, the numerical values are set up according to the following two rules:

 

1. For the first literal: if it is unassigned, set its value to O.

2. For any other literal: if it is unassigned, then set its value to one greater than the value of the preceding literal.

 

From these two rules, it can be seen that DAYS.Monday will be set to 0, and the values increased until DAYS.Sunday is set to 6. Note also how we are referring to these values – the values specified in an enumeration are static, so we have to refer to them in code using the name of the enumeration: "DAYS.Monday" rather than just "Monday". Furthermore, these values are final-you can't change their runtime value.

~

The following code demonstrates how you can override the default setting which makes the default values integers. In this example, the enumeration values are set to bytes.

 

enum byteEnum : byte

{

A,

B

}

 

You can also override the default numerical values of any and all of the enumeration elements. In the following example, the first literal is set to value 1. The other literals are then set up according to the second rule given above, so DAYS.Sunday will end up equal to 7.

 

public enum DAYS

{

Monday=!,

Tuesday,

wednesday,

Thursday,

Friday,

saturday,

Sunday,

}

 

In the two examples given, the values of each literal has been' unique within the enumeration. This is usually how you will want things to be, but in fact the values need not be unique. In the following case, the value of DAYS.Thursday is also set to equal!. The values assigned to the other literals will follow the rules given previously, so both DAYS.Tuesday and DAYS.Friday will equal 2, etc.

public enum DAYS

{

Monday=l,

Tuesday,

wednesday,

Thursday=l,

Friday,

Saturday,

Sunday

}

In C# enumerations are type-safe, by which we mean that the compiler will do its best to stop you assigning implicit values to enumeration typed variables. For instance, the following code should not compile:

 

int i = DAYS.Monday;

DAYS d = i;

In order to get this code to compile, you would have to make explicit casts both ways (even converting from DAYS to int), ie:

 

int i = (int)DAYS.Monday;

DAYS d = (DAYS)i;

At this point you may be wondering what happens if you cast an int to an enumeration value where that same value is defined for two elements within the enumeration. And the answer is this: one of the elements is given 'primary' status, so it gets picked ahead of the other.

A useful feature of enumerations is that one can retrieve the literal as a string from the numeric constant with which it· is associated. In fact, this is given by the default To String() method, so the following expression comes out as true:

DAYS.Monday. ToStringO=="Monday"

The following code prints out both the literal and its constant value for the specified enumeration.

 

Example of Enumeration

 

class EnumTest

{

public enum DAys:byte {MON,TUE, WED,THU,FRI,SAT,SUN}

public static void Main(string[] args)

{

DAYS d=EnumTest.DAYS.SAT;

byte a=(byte) EnumTest.DAYS.SAT;

string str=EnumTest. DAYS. SAT.ToString() ;

consol e. writeline("{O}, {I}, {2}" ,d, d. ToString() ,d. Tostring("d")) ;

consol e. writeline("{O}, {l}",str, a) ;

}

}

 

OUTPUT:

SAT, SAT, 5

SAT, 5

Since it's not immediately obvious what's going on in the main method here, let's take the time to go through it.

console.Writeline("{0}, {I}, {2}" ,d, d. ToString() ,d. Tostring("d")) ;

And what the String.Format method does is to take 'textual representations' of the objects it is passed as parameters, and slots them into the appropriate places within the 'format string' it is passed.

Now, we've already noted that ToString () will return a literal string, but what about the method d.ToString ("d")?

The ToString method can take a single IFormatProvider parameter which indicates how the string conversion should be conducted. Values for this parameter can include things like "g", "d", "x", "f', etc. The stated implication of "d", however, is to render in 'Decimal format'.And when we use this on an enumeration member, it provides a string representation of the "numerical value" of the enumeration member.



About Dinesh Thakur

Dinesh ThakurDinesh Thakur holds an B.SC (Computer Science), MCSE, MCDBA, CCNA, CCNP, A+, SCJP certifications. Dinesh authors the hugely popular blog. Where he writes how-to guides around Computer fundamental , computer software, Computer programming, and web apps. For any type of query or something that you think is missing, please feel free to Contact us.