AbstractEnumerations are a commonly used and known tool built into programming languages. They are however not the only way of doing things. In this article, Brendan outlines an alternative to using enumerations. He discusses some of the reasons why you might use an alternative and uses source code to explain how to achieve a comparable equivalent to enumerations.

Enumerations are one of many useful tools at the fingertips
of developers. Most of us have been using them for years. Because they are so
common and easy to use, I believe we sometimes overuse them. An enumeration at
its core is merely a number value. Try this in C# and you will see how easy it
is to convert between enumerations and integer values. Enumerations are useful
because they are named collections of values which are associated under the
surface with integer values.

This example shows how one could quickly and easily print
out the names and number values of an enumeration.

That is some very easy code, and I think most people have
probably seen enumerations printed by name as well enums converted to integers
and their values used. So if enums are so great, why would I write an article
talking about a way of avoiding them? Well, I would first like to say that I am
a big fan of enums. I think they are quite useful; they just have some risks as
well as some circumstances under which they fall a little bit short as far as
software design is concerned.

Title:
good
Name:
msoni
Date:
2010-01-05 8:31:11 AM
Comment: i like it ......its a very nice aricle

Title:
Good Article/Weak Arguments
Name:
Clint
Date:
2009-03-25 2:13:49 PM
Comment: Good article on the usage between objects/enumerators; weak arguments on the reasons. The statement "If there will be a good amount of logic based on these use an enumeration or an object. Do not have a lookup table. I know it is nice for the database, but any changes to the database will cause problems in the code." collapses on itself. Regardless of implementation, values that dictate logic will usually have a negative impact on the coded logic.

Basically you can read this line like this, "find from items.ToArray() an item where that item's Name property is equal to the variable name".

The variable "instance" is defined at that point to represent any item in the array we are searching.

I hope that answers your questions.

Title:
Two questions
Name:
Amir Arjmand
Date:
2009-02-17 9:54:41 PM
Comment: Hi everyone, I am new to C# and as I was going through the sample code, I came upon this statement

instance => instance.Name == Name

Could anyone please explain how exactlly this part of the code works. Where does that instance come from and what is => ? I could not find anything regarding that operator in msdn library for C#?

Thanks;

Title:
RE: hmm
Name:
Brendan Enrick
Date:
2009-02-17 8:50:19 AM
Comment: @dario-g thank you for making sure I don't pass along misinformation. Although, there is a big difference between casting an integer as an enum and passing an out of range integer to this object. If you read the code listing 6 above, you will notice that the object throws an argument out of range exception if the integer is out of range. This means that you will know there is a problem and what the problem is. You can also easily handle this exception.

If you used the enum, you might not run into any exceptions, and maybe later the code will save the integer value of the enum. You are going to have an invalid result stored somewhere and possibly not even know.

Thank you for the comment. Feedback is always welcome and appreciated.

Title:
Mousover
Name:
Brendan Enrick
Date:
2009-02-17 8:42:47 AM
Comment: @Alex Simkin Yes, you are right the debugger works very well with enumerations. However, if you move the mouse a little bit more to the plus sign, it will reveal the values of the properties of this object. This will give you access to not just the name, but also the value of the object. This is more than you would get from an enum.

Title:
Mr. ?
Name:
Alex Simkin
Date:
2009-02-16 2:35:11 PM
Comment: In debugger, if you mouseover variable of enum type, you will find out the value instantly. If you mouseover MyObject variable, you will only see that it contains MyObject instance.