Yes, you can see similarities between the behavior
of Enum and Const: both are immutable (unique, unchanging).
But, a Const is a simple Field that holds only certain basic Types: int, double, string. At compile time, any reference (use of its name/label) to the Const will be replaced by the value
of the Const.
An Enum is a special Type (and a ValueType) of structure/class; like a Class, it can be defined in a NameSpace scope, or defined inside another Class. If you put it inside a Class, you will access it by: ClassName.EnumName ... the same way you would access any Class member defined as static,
I like to think of an Enum as an immutable Dictionary whose Key/Value pairs are Labels (strings) and ValueTypes (int, double, byte, etc.).
Enum also supports bitwise
operations if it is adorned with the [Flags] attribute [^
public enum FlagEnum
caseNone = 0,
case1 = 1,
case2 = 2,
case3 = 4,
case1And2 = case1 | case2,
case1And3 = case1 | case3,
case2And3 = case2 | case3,
caseAll = case1 | case2 | case3
With .NET FrameWork 4, the 'HasFlag method is available for an Enum with the [Flags] attribute: [^
]. Note: using the None value in a bitwise and
operation will always result in a value of #0.
Enum GetValues and GetNames methods allow access to the internal collections of labels and values in an Enum: I suggest you examine these when that kind of need arises.
There have been many criticisms of the implementations of HasFlag, GetValues, and GetNames. And, there are popular extensions available to handle their .. uhhh ... quirkiness. Just search on something like ... C# Enum GetValues alternative ...