Advantage of Using Enum in Real Life Situation

In this article I have given some of the advantage of using Enum and also given some of the points to remember while handling Enum. Enum always makes life easier for developer and also helps the stranger while reading code.

Advantage of using this

  1. Enums are not allocated in memory. They exist only on compilation stage. When code runs - there is no enums there anymore.

  2. If you Writing the Architectural Code, then You have to go for Enum not Const variable String like 'const sting Word="Word"'

  3. In my example I have used the MS product like Word, Excel etc, it clearly tells you, that you have a definite choice of product in the application, not more than that. One more thing, if new person wants to add a new product, he or she clearly knows where to add this product and how it can be used.

  4. Enum.Parse (), this is very useful, if you consider the web application if the query string "product=word" in the URL, then we use to Get Enum and use throughout the Application, it is helpful.

  5. Main thing is Readability, you should always remember this.

  6. Enum.Parse throws error if value is incorrect, so we use Enum.IsDefined to check. So every helper method in Enum helps a lot.

  7. If you have choice like list of action or options or possibilities, then you can go for Enum. eg. FileAccess (Read, Write etc).

  8. You will get error in compile time itself not run time, it is better and best practice.

  9. As you know it is a value type and also we can customize the Enum value as mentioned in this article.

  10. It reduces the chance of Bug in code something like case sensitive problem.

  11. IntelliSense makes you happy to work as you know how much it is helpful while programming. This is also the best use of Enum. Once place  we use Word, in other place we use word, it is inconsistent too. If you think of it, it helps a lot. You don't have to remember the string fully , just type Enum and Make a dot it shows the list.

Summary

List of action or options or possibilities then we can use then best practice is always enum.

Related Article

How to Play With Enum in C#

Ebook Download
View all
Learn
View all