Wrapping my head around Enums in C#

As I told myself the little C# I know, it was recently pointed out to me that I use “Magic Numbers” a term new to me till a few days ago.

As is Enums, Today, I am trying to understand how I can change some of my practices over to a more standard method. I read up on this method and watched some video tutorials but as the concept is very new to me, I am failing at implementation.

I am trying to control a UI menu using Enums instead of “Magic Numbers”. I really like the idea of being able to use names and the fact that enums are kind of like static variables as they are not confined to single script in the project.
Here is how I would control this type of menu in the past

// First I would create an array of Game Objects that hosts the buttons to be turned on or 
// off.
public GameObject[] Menus;

//once that is populated, I would activate individual menus like so

//Which would call the following function
public void ActivateMenu(int magicNumber){
    int runCount = 0;
    foreach(GameObject i in Menus){
          if(runCount == magicNumber)

Now I am trying to acomplish the same thing with Enums but I get stuck very early.

public enum Menus {Main,Options,Controls};
public Menus.Main;

I guess my question is, How would I tell elements in my enum to be a specific game object and then how would I iterate through the enum to not only do something to it, but do something else to every element that is not it.

Or em I thinking about this the wrong way? Is this not an appropriate use of enums? Do I have it too closely linked to arrays in my head?


As far as I’m aware you cant map enums directly to GameObjects. The following from the MSDN page on Enums in C# should explain why.

" Every enumeration type has an underlying type, which can be any integral type except char."

So mostly just ints. What you could do is use enums to replace the index value that you are passing in. (by default i think enum mapping values start at 0 and then increase 1,2,3 etc for each item after them, But you can manually specify values for each entry yourself).

However with that approach, it wouldn’t really serve you well if you’re then going through your entire array to disable everything else anyways.

There’s no problem using enum values as index into an array, but you have to understand that an enum is it’s own type even when it uses an int in the background. C# uses strong types so using an enum as integer requires a cast, nothing more:

public enum EMenu {Main = 0,Options = 1,Controls = 2};
public GameObject[] MenusGo;

public void ActivateMenu(EMenu aMenu)
    for(int i = 0; i < MenusGo.Length; i++)
        MenusGo*.SetActiveRecursively(i == (int)aMenu);*