What is the difference between an enum and an array? with both you can store elements the main difference between arrays and enums are that enums are not index based wereas arrays are, that is all I know.

So I have a few questions

  1. How do we use enums?
  2. What are enums for?
  3. Where would we use an enum instead of an array

EDIT: Sorry I overlooked the C# context. And replied in c++ like. Though the fundamentals are probably the same.

Answering with my limited knowledge of enums:

Enumerations exist mostly for readability. There might be a few special cases that you can't achieve without enums, but mostly it's a special array.

You declare an enum like this:

enum type_name {

and declare something like:

enum colors_t {black, blue, green, cyan, red, purple, yellow, white};

though you could replace this with an array:

int colors[8] = {0,1,2,3,4,5,6,7};

How would you remember which number represents which value, when you see an arbitrary line in your code:

if (some_color == 0) //??? What's 0? then you go searching for the declaration

with enums it's more readable:

if (some_color == black){
    //do something here

There even exists an implicit conversation to int from enums, so things like these are still valid:

some_color == 0;


switch( some_color){
    case black:
    case green:

In general you would use them, where some of your data can only take a few specific values, but want more readability.

Good explanation.
This article has been dead for over six months. Start a new discussion instead.