Enumerations and constants. What to use when?

I read about enumerations and found them very similar to declaring constants. How do I know when to use a constant, not an enumeration or vice versa. What are some of the benefits of using enums?

+82
language-agnostic enums c #
Mar 05 '09 at 7:04
source share
6 answers

Use enumerations when you want to define a range of values; something can be. Color is an obvious example, for example:

public enum Colour { White, Red, Blue } 

Or maybe a set of possible things like: (An example that I stole from here , since I'm lazy)

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

Constants must be for a single value, such as PI. There is no PI range, only PI.

+80
Mar 05 '09 at 7:13
source share

What is missing in the other answers is that enums are of an integer base type. You can change the default value from int to any other integer type except char, for example:

 enum LongEnum : long { foo, bar, } 

You can explicitly specify and implicitly the base type, which is useful in switch statements. Beware that any value of the base type can be enumerated, even if the enumeration does not have a member with the corresponding value. Therefore, always using the default switch is a good idea. BTW, .NET itself allows even overridden floating point values, but you cannot define them in C #, although I think you can still use them (except for the switch).

In addition, using enumerations gives you more type safety. If you intend to use, for example, int constants as method parameters, then I could call the method with any int value. Of course, through casting, this can also happen with transfers, but this will not happen by accident. Worse is the ability to confuse the order of parameters.

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

If the constant A can go only to a, and the constant B can go only to b, then using two different types of enumerations will detect any abuse at compile time.

+27
Mar 05 '09 at 16:22
source share

A constant is a language function that says that a variable will not change the value (therefore, the compiler can do optimizations around this knowledge), where the enumeration is a specific type.

Constants can be any data types, but an enumeration is an enumeration.

I use enumeration anywhere where you can have several options and want to improve code readability. that is, you could have trace levels as int with values ​​0, 1, 2, or as an enumeration as an error, warning, and information.

Enum can also be used as bitwise operators, that is, FontStyle.Bold | FontStyle.Italic will provide you with bold and italic fonts.

+19
Mar 05 '09 at 7:09
source share

In addition to Robert's answer:

  • Use an enumeration for a finite set of named values. You do not care about the numerical value behind each character (but you still have the opportunity to impose them, for example, for compatibility with the legacy system).

  • Robert: Yes, Enum can be used as bit fields. Use the Flags attribute (and make sure that the members of the enumeration have appropriate numeric values).

+8
Mar 05 '09 at 7:14
source share

The C # constant is like a variable because it gives a specific name to a value. However, the constant is different from the standard variable, because after its definition, the value assigned to the constant can never be changed. The main advantage of constants is their help in creating self-documenting code, and also allows you to declare key values ​​in one place, which makes it easy to maintain if the value needs to be updated and the software is recompiled.

While Enumerator lists are useful for defining sequences and states, especially when there is a natural progression through these states. This is due to the fact that each constant in the list can be formatted and compared using either its name or value. An enumeration can also be used to define a limited set of valid values.

+3
Mar 05 '09 at 7:16
source share

One thing that I find convenient when using enum instead of const is that you can iterate over values ​​in enum , it is much harder to do with const .

0
Apr 05 '19 at 0:06
source share



All Articles