Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Enums and Constants. Which to use when?

I was doing some reading on enums and find them very similar to declaring constants. How would I know when to use a constant rather than an enum or vice versa. What are some of the advantages of using enums?

like image 591
Draco Avatar asked Mar 05 '09 07:03

Draco


People also ask

Should I use a constant or enum?

Enums are lists of constants. When you need a predefined list of values which do represent some kind of numeric or textual data, you should use an enum. You should always use enums when a variable (especially a method parameter) can only take one out of a small set of possible values.

When should we use enum?

Enums are used when we know all possible values at compile-time, such as choices on a menu, rounding modes, command-line flags, etc. It is not necessary that the set of constants in an enum type stay fixed for all time. In Java (from 1.5), enums are represented using enum data type.

What is the difference between enum and constant?

The only difference is that enum constants are public , static and final (unchangeable - cannot be overridden). An enum cannot be used to create objects, and it cannot extend other classes (but it can implement interfaces).

Are enums constants?

An enum type is a special data type that enables for a variable to be a set of predefined constants. The variable must be equal to one of the values that have been predefined for it.


2 Answers

Use enums when you want to define a range of values that something can be. Colour is an obvious example like:

public enum Colour {     White,     Red,     Blue } 

Or maybe a set of possible things like: (Example I stole from here as I'm lazy)

[FlagsAttribute] enum DistributedChannel {   None = 0,   Transacted = 1,   Queued = 2,   Encrypted = 4,   Persisted = 16,   FaultTolerant = Transacted | Queued | Persisted } 

Constants should be for a single value, like PI. There isn't a range of PI values, there is just PI.

Other points to consider are:

  • a: Constants don't necessarily indicate a relationship between the constants, whereas an enumeration indicates that something can be one of the set defined by the enum.
  • b: A defined enumeration can help you with type checking when used as an argument. Constants are just values, so they don't provide any additional semantic information.
like image 132
Andrew Barrett Avatar answered Sep 29 '22 17:09

Andrew Barrett


What's missing from the other answers is that enums have an integer base type. You can change the default from int to any other integral type except char like:

enum LongEnum : long {     foo,     bar, } 

You can cast explicitly from and implicitly to the the base type, which is useful in switch-statements. Beware that one can cast any value of the base type to an enum, even if the enum has no member with the appropriate value. So using always the default section in a switch is a good idea. BTW, .NET itself allows even floating point valued enums, but you can't define them in C#, although I think you can still use them (except in switch).

Furthermore, using enums gives you more type safety. If you intend to use e.g. int constants as method parameters, then I could call the method with any int value. Granted, via casting it can happen with enums, too, but it won't happen accidentally. Worse is the possibility to confuse the order of parameters.

void method(int a, int b) {...} 

If constant A only may go into a and constant B only may go into b, then using two different enum types will uncover any misuse during the compilation.

like image 25
Johannes Avatar answered Sep 29 '22 15:09

Johannes