Enums (basics, advanced)

Enumerations offer an easy way to make your code legible. We are far away from Assembler programming and use words instead of numbers when possible. In an enum each word represents a number.

enum eOrder { Salad, Pizza, Spaghetti}

By default the first element (Salad) has the value of 0. Each following value is incremented by 1 (Pizza = 1, Spaghetti = 2).
You can define a value explicitly, otherwise it follows the standard increment.

enum eOrder { Salad = 100, Pizza, Spaghetti =  300}

static void Main(string[] args) {
    eOrder lOrder = eOrder.Salad;
    Console.WriteLine(lOrder + " :" + (int)lOrder);
    Console.WriteLine(eOrder.Pizza + " :" + (int)eOrder.Pizza);
    Console.WriteLine(eOrder.Spaghetti + " :" + (int)eOrder.Spaghetti);
    Console.ReadLine();
} //

example output:
Salad :100
Pizza :101
Spaghetti :300

You can also predefine the value type. The default is integer.

enum eOrder : byte { Salad = 100, Pizza, Spaghetti};

static void Main(string[] args) {
    eOrder lOrder = eOrder.Salad;
    Console.WriteLine(lOrder + " :" + (byte)lOrder);
    Console.WriteLine(eOrder.Pizza + " :" + (byte)eOrder.Pizza);
    Console.WriteLine(eOrder.Spaghetti + " :" + (byte)eOrder.Spaghetti);
    Console.ReadLine();
} //

example output:
Salad :100
Pizza :101
Spaghetti :102

And when you have to save space on the hard disk/in RAM or you need smaller packages to send across a network, then you can use bit fields where applicable. Simply add the “Flags” property.

[Flags]
enum eDays {
    None = 0x0,
    Monday = 0x1,
    Tuesday = 0x2,
    Wednesday = 0x4,
    Thursday = 0x8,
    Friday = 0x10,
    Saturday = 0x20,
    Sunday = 0x40,
    Weekend = 0x60
};

static void Main(string[] args) {
    eDays lDays = eDays.Saturday | eDays.Sunday;
    if (lDays.HasFlag(eDays.Saturday)) Console.WriteLine("Saturday is set");
    if (lDays.HasFlag(eDays.Sunday)) Console.WriteLine("Sunday is set");
    if (lDays.HasFlag(eDays.Sunday | eDays.Saturday)) Console.WriteLine("Weekend is set");
    if (lDays.HasFlag(eDays.Weekend)) Console.WriteLine("Weekend is set");
    Console.WriteLine(eDays.Weekend + " : " + ((int)eDays.Weekend).ToString("X2") + " == " + ((int)lDays).ToString("X2") );   // X2 => hexadecimal using 2 alphanumeric places
    Console.ReadLine();
} //

example output:
Saturday is set
Sunday is set
Weekend is set
Weekend is set
Weekend : 60 == 60

The next challenge for today is parsing strings:

static void Main(string[] args) {       
    eDays lDays = (eDays)Enum.Parse(typeof(eDays), "Thursday");  // needs try/catch block
    Console.WriteLine("first parsed result is : " + lDays);

    // does not need a try/catch block
    if (!Enum.TryParse("Friday", out lDays)) Console.WriteLine("Parser failed");
    else Console.WriteLine("second parsed result is : " + lDays);

    Console.ReadLine();
} //

example output:
first parsed result is : Thursday
second parsed result is : Friday

And now let’s parse integers and also iterate through all available enum values. If you assign arbitrary integers to enums, they will simply store the value. There is no validity check.

static void Main(string[] args) {
    eDays lDays = (eDays)8;
    Console.WriteLine("result 1 is : " + lDays);

    lDays = (eDays)0x80;
    Console.WriteLine("result 2 is : " + lDays); // oops, doesn't fail?            

    lDays = (eDays)0x140;
    Console.WriteLine("result 3 is : " + lDays); // doesn't fail again
    eDays[] lAllPossibleDays = (eDays[])Enum.GetValues(typeof(eDays));
    foreach (eDays x in lAllPossibleDays) {
        if (((int)lDays & (int)x) == (int)x) Console.WriteLine(x); // code not optimized in demo
    }

    Console.ReadLine();
} //

example output:
result 1 is : Thursday
result 2 is : 128
result 3 is : 320
None
Sunday

About Bastian M.K. Ohta

Happiness only real when shared.

Posted on December 30, 2013, in Advanced, Basic, C# and tagged , , , , . Bookmark the permalink. 1 Comment.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: