Erik
Erik

Reputation: 954

Another enum vs. int

I need namespace-wide contants. So I have the option to use enums, like:

public enum Token
{
    INFORMATIONAL,
    WARNING,
    ABORT,
    FATAL
};

But the disadvantage is, in WPF for example, that I need to convert them to int, now and then.

So I thought, why not

public static class Token 
{
    public const int
    INFORMATIONAL = 0,
    WARNING =1,
    ABORT = 2,
    FATAL = 3;
};

in the source, they look the same AND I have no conversion issues. So what would be the reason for enums at all? I'm clearly missing something. What is the reason for enums?

Upvotes: 1

Views: 1321

Answers (2)

user12031933
user12031933

Reputation:

Consideration

With enum you define a type having some constants themselves "typed" (not the good word by I don't know how to say that).

The advantage between using enum values and constants is to be able to parse the values and to use the enum scope as a type.

So it is a better abstraction.

Also you don't need to care to define the values even if you can.

For example:

Parsing enum values

foreach ( var value in Enum.GetValues(typeof(Token) ) ...

Passing enum parameter

void Method(Token token) ...

Declare a collection keyed by an enum

var map = new Dictionary<Token, string>();

Condition test

if ( value == Token.INFORMATIONAL ) ...

Easy of bit flags usage

https://learn.microsoft.com/en-us/dotnet/api/system.flagsattribute

Default item name ToString result

Console.WriteLine(Token.INFORMATIONAL)

Will display the name of the value, not its value as for a constant.

So to display the value:

Console.WriteLine((int)Token.INFORMATIONAL)

Conclusion

With constants, you need to use reflexion to parse a list but it is ugly and not usefull at all and you can't pass them as a typed parameter.

Usage possibilities of constants is limited.

So enums and constants are not the same thing at all even the basic as int usage.

Thus using enum or constants depends on the usage you will do.

If you only need int values and don't care about segregation, choose constants.

If you care about type safety and need to do advanced things, choose enum.

One last word to say is that enum is to not care about the underlying value, so if you need to work with values, use constants, else enum.

Use what is the most natural thing.

Upvotes: 3

Christopher
Christopher

Reputation: 9804

Enums to my knowledge have only one advantage: Type Safety.

Edit: They also have that [Flag] option, wich helps with encoding a lot of settings in one variable. Thanks to John for reminding me.

There is this interesting article about Primitive Obession (and I am really surprised how often I got to link this since learned about it yesterday).

It applies to your case like this: If you just use Primtives everywhere, the compiler can not warn you as much. You might try to assign 42 to a variable that was only supposed to one of those constant values.

But trying to assign anything but valid values to a Enum? Pretty hard/impossible to do by accident.

Upvotes: 5

Related Questions