views:

713

answers:

5

Hi all,

Okay this may seem like a silly question but I'm fairly new to all this. 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? I never used them before. I'm working with c#.

+3  A: 

A constant is a language feature which says that the variable won't change value (so the compiler can do optimisations around that knowledge) where an enum is a specific type.

Constants can be any data type but an enum is an enum.

I use an enum any place where you could have a number of options and want to improve readability of the code. i.e. you could have trace levels as an int with values 0, 1, 2 or as an enum as error, warning and info.

Enum's also have the ability to be used as bitwise operators, i.e. FontStyle.Bold | FontStyle.Italic would give you bold and italic fonts.

Robert MacLean
+8  A: 

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.

Andrew Barrett
+3  A: 

In addition to Robert's answer:

  1. Use enum for a finite set of named values. You don't really care about the numerical value behind each symbol (but you still have the ability to impose them, e.g. for compatibility with a legacy system).

  2. Robert: Yes, Enum's can be used as bit fields. Use the Flags attribute (and make sure members of the enum have suitable numerical values).

Serge - appTranslator
+1  A: 

A C# constant is similar to a variable in that it gives a defined name to a value. However, a constant differs from a standard variable because once defined, the value assigned to the constant can never be changed. The chief benefit of constants is their assistance in creating self-documenting code as well as allowing the declaration of key values in a single place, which permits easy maintenance should the value need to be updated and the software recompiled.

Whereas Enumerator lists are useful for defining sequences and states, particularly when there is a natural progression through those states. This is because each constant in the list can be formatted and compared using either its name or value. An enum can also be used to define a limited set of valid values.

simplyharsh
+2  A: 

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.

Johannes