Home » c# » CSharp Enum

CSharp Enum

tp_vol4_002 It is often tempting when working with your code to test against static values in your system.  For example:

if (i == 1)
{
    // do something
}
else if (i == 2)
{
    // do something
}

Or, if you are a bit more clever, you use a switch statement instead.  But neither of these methods tell you what 1 and 2 actually represent.

Now, you could use constants:

const int Thing1 = 1;
const int Thing2 = 2;

var i = 1;
if (i == Thing1)
{
    // do something
}
else if (i == Thing2)
{
    // do something
}

Which goes a bit further.  But if I need to use Thing1 and Thing2 in other sections of my code, I’m kind of stuck.  Oh, I guess one could use a class with properties and evaluate against them, but there is a better way.

Enums.

When you have a set of items that are all related to each other, you can create an enumeration (enum) to represent them as a set.  There are several of these built into the .NET Framework.  One of the most common is the DialogResult enum that holds all of the return values for a DialogBox.

We can create our own by either placing code similar to the following in an existing file or in a new code file:

enum Thing
{
    One,
    Two
}

Which can then be used in our code as:

switch (i)
{
    case Thing.One:
        // do something
        break;
    case Thing.Two:
        // do something
        break;
}

In the example above One represents zero and Two represents the value of one.  But it really doesn’t matter because we are always going to reference the value by name.

You are not stuck with having to have Thing.One be one.  You can assign a value to it or any of the other values.

enum Thing
{
    One =2,
    Two
}

In the code above, One equals the value 2 and Two equals 3.  Each item in the list is automatically incremented by one unless it is overwritten with a value.

In the case of items that need to be bit representations you can do something like:

enum Thing
{
    BitOne = 1,
    BitTwo = 2,
    BitThree = 4
}

And then your code can evaluate them using bit wise and statements:

if ((i & Thing.BitTwo) > 0)
{
}

 

This will be true if i is equal to 2, 3, 6, or 7.  The expression above is a lot easier to evaluate than “if i is equal to 2, 3, 6 or 7.”

Aside from the practical ease of coding that enums provide, it also makes the code much clearer.  Which would you rather do?  Lookup what the return value for OK is or just have intellisense provide it to you from the DialogResult enum?  Obviously, having it provided by its name is much easier.  You should provide the same benefit to any future developer who works on YOUR code.

 

Other post in c#

Related Post

  • && vs & and | vs ||… What’s the difference?&& vs & and | vs ||… What’s the difference? It seems like such a trivial thing to be talking about but not knowing the difference between && vs & or || vs | can make a huge difference between working code and code that only seems […]
  • What is the global keyword in CSharp?What is the global keyword in CSharp? During the Christmas break, I received the following question:What does C# global keyword actually do? Code example, from table adapter […]
  • Unsafe Mode in C#Unsafe Mode in C# One of the "advantages" of using CSharp instead of VB.NET is that if programmers want to, they have the option of bypassing the memory management of .NET and working with memory […]
  • Upload a File via WebRequest Using CSharpUpload a File via WebRequest Using CSharp I got this question a couple of weeks ago but just never had the time to put into answering fully.  But today I have some extra time due to the fact that I’m under-booked with […]
  • CSharp Numeric OverflowsCSharp Numeric Overflows Did you know that when you are dealing with numbers, by default, .NET will do, or try to do exactly what you tell it to do?  If you tell it to do the impossible, it will do the next […]

About Dave Bush

Dave Bush is a Full Stack ASP.NET developer focusing on ASP.NET, C#, Node.js, JavaScript, HTML, CSS, BootStrap, and Angular.JS.Does your team need additional help in any of the above? Contact Dave today.

One Pingback/Trackback