Another noobish question..Why enumeration instead of array.

Hello again.

So I’m reading through my C# book and it is up to enumerations now. So far the book as been pretty good but it just seemed to skim through enumerations and I just can’t seem to make out the difference between an Array and Enumeration (which just seems like a fancy name for an Array).

A vague question, but what am I missing here. What does an Enumeration do that an Array doesn’t?

Again thanks in advance for your help.

I think your book not only skimmed over enumerations but must have described them poorly. They are actually two very different things, and don’t really act as solutions to any of the same problems unless you’re misusing one of them.

An enumeration is more akin to an int than an array, actually. (Behind the scenes, it IS an int, even.) If you imagine that you gave specific numbers individual labels, that’s all an enumeration is. So in this:

public enum AnimalType {Cow, Dog, Cat};

Cow is equivalent to 0, Dog is equivalent to 1, Cat is equivalent to 2. If you did this:

AnimalType thing = AnimalType.Cow;
int thingAsANumber = (int)thing;
Debug.Log("A cow is number "+thisAsANumber);

You’d see the number value “behind” the enum (which I believe will be 0 in this case).

You can also manually assign a value to an enum, though the times you’d actually want to do this are rare:

public enum AnimalType {Cow = 2, Dog = 10, Cat = 12};

So what enumerations are good for is when you have one of a limited set of options. In Unity, a public member that’s an enum will also conveniently show up as a drop down menu in the inspector.

Does that help?

1 Like
enum Weekdays {
Sunday, Monday, Tuesday, Wednesday, Thursday, Friday, Saturday
}

Enums can be out of order, also. If you assign Tuesday = 5, then Wednesday is 6 and so on.
Honestly, they’ve always seemed to me as a a little more “readable” for the coder, and they are also fixed in size…
For example, in a switch statement, you need only fill in as many values as the enum definition contains, and not include a ‘default’ statement.
Enums can also be used as flags, which you can ‘OR’ or ‘AND’ together… I don’t want to babble on too much, sorry :slight_smile:

1 Like

I’m guessing you misused the word enumeration when you meant enumerable.

An enumerable is something that can be enumerated.
An array is a series of items, typically placed side by side in a block of memory.

The difference is subtle but important. An enumerable is a generalization. An array is an enumerable but an enumerable is not limited to being an array. There are many types of data graphs, many of which can be conceptualized as an enumerable.

An enumerable is simply a way of saying, “this is a group of items, and you can look at each one.”

3 Likes

Nice catch, @eisenpony - I hadn’t even considered that, but that is far more likely (and a good explanation).

1 Like

Thanks @StarManta ! and did you notice I originally messed up the words and started using enumeration when I meant enumerable?.. I’ve made the same mistake many times myself, which is why I suspected it.

2 Likes

lol that’s great. I’m glad someone mentioned that. I thought of it, was considering writing about it, but didn’t. At least the OP has both options to read now :slight_smile:

I’ve never mixed up enumeration (if only because I always call them just “enum”), but a few of my early coroutine attempts were typed IEnumerable (instead of IEnumerator), so I know the pain…

Thanks for the replies StarManta, methos5k & eisenpony.

I think eisenpony may have it right why it’s confusing, it is subtle.

I’ve used arrays of int’s before then match it with a switch statement and seems to do the same thing, or similar, but I want to understand the difference. On the surface it seems like an array of strings with a built in switch statement.

The book explained it similarly as you did StarManta, just used seasons instead of animals, perhaps as I get further in it will use some more examples to make it clearer.

I don’t think the book used the word enumerable, but I will check when I get home and report back.

Then I was wrong, you got the word right.

Go back and read StarManta’s first response. Enumerations and arrays are very different things and it will serve you well to understand why.

Think of an enumeration like you would think of integers (a special, named subset of integers).
Think of arrays as containers to hold them (or many other things).

I generally think of enums as a series of const values, grouped together logically, and with human readable names.

Where you could do something like this:

public const int dog = 1;
public const int cat = 2;
public const int rat = 3;
public const int rabbit = 4;

private int pet;

public void OnCatButtonPress()
{
    pet = cat;
}

With an enum you can make it a bit cleaner:

public enum Animal { dog, cat, rat, rabbit };

private Animal pet;

public void OnCatButtonPress()
{
    pet = Animal.cat;
}
2 Likes

Thanks again. I think I’m understanding better now. That is a good explanation Joe-Censored.

I’ve known the theory of how constants work for awhile, but haven’t really used them much, if at all, in my little programs I’ve been writing. That may be a big part of my issue in understanding. I just need to play around with them for a bit and get it down.

Thanks again to all.

Welcome :slight_smile:

The major benefit is not necessarily readability, as you can simply do the same with a static class + const variables and it’d kinda look the same.

The actual idea behind it comes from the type/value-safety’s perspective.

Consider this, someone could pass any integer value into the method without doing anything horribly wrong (except not reading the documentation carefully).

public void SomeMethod(int value) { /* ... */ }

Now take a look at this:

public enum SomeEnum { E1, E2, E3 }
public void SomeMethod(SomeEnum someEnum) { /* ... */ }

It’s more restrictive if used correctly, though not 100%. When using the type required by the parameter (without casting), you only have the given set of named values.
Of course you can still start to randomly and wildly cast values to that enum type, since they’re actually treated as the underlying integral type (byte, short, int, long, …) but well, that’d be your own fault.

2 Likes