Enumerations
An enumeration is a set of named integer constants.
Enumerations are value types
Enumerations are especially suitable for representing types that can take one of a set of fixed values.
They must always be fully qualified with the enumeration type name.
The default type is "int" although the type can also be (byte, sbyte, short, ushort, uint, long, ulong)
public enum enShapes // defaults to int
{
Triangle,
Square,
Rectangle,
Circle,
Unknown,
[Stop] //Stop is a reserved word
}
enShapes Shape = enShapes.Triangle;
byte Data Type
public enum enShapes : byte
{
Hexagon
}
GetName Method
This retrieves the name of the constant instead of the numerical value
System.Windows.Forms.MessageBox.Show(System.Enum.GetName(typeof(enShapes,2);
If numerical values are not specified then
1 plus the previous constant is used
0 is used for the first constant
Enumerations are value types and they differ from the abstract System::Enum class which in turn derives from System::ValueType
Any Enum you define in your application derives from the System.Enum, which in turn inherits from System.ValueType.
If no explicit value is provided for the first member inside the Enum block is assigned the value 0, the second is assigned 1, etc
You are encouraged not to change the initial value.
Enum blocks can appear anywhere in a source file, an inside module, a class or a structure block or directly at the namespace level
GetNames Method
This returns a string array of the names of the constants in the enumeration.
String Enumeration
public interface IStringEnumeration
{
String.ToString();
}
public abstract class StringEnumeration<T> : IStringEnumeration
where T : StringEnumeration<T>
{
private string myString;
public StringEnumeration(string sStringConstant)
{
myString = sStringConstant;
}
public override string ToString()
{
return myString;
}
}
public sealed class MyEnumerations : StringEnumeration<MyEnumeration>
{
public static readonly MyEnumerations MyString1 = new MyEnumerations("MyString1");
public static readonly MyEnumerations MyString2 = new MyEnumerations("MyString2");
private MyEnumerations(string sEnumeration)
: base (sEnumeration)
{
}
}
Flags Enumeration
You can use enumeration constants to represent bit flags.
You can attach an attribute System.FlagsAttribute to an enumeration
You can use enumeration constants to represent bit flags
Use the System.FlagAttribute attribute
[System.Flags]
[System.FlagsAttribute]
{
level1 = 0x0,
level2 = 0x1,
level3 = 0x2
}
<System.Flags>
<System.FlagsAttribute()>
Bitwise Enumeration
A bitwise enum is an enum type that uses bitwise operations to combine multiple enum values into a single value.
Each enum value is assigned a unique bit flag, represented by a power of 2.
By assigning these flags, we can represent multiple enum values by combining or masking them using bitwise operators.
Defining Bitwise Enums
To define a bitwise enum, we need to decorate it with the [Flags] attribute, indicating that the enum values can be combined using bitwise operations. Here's an example,
The DaysOfWeek enum has every day of the week assigned a unique power of 2.
The None value is assigned 0, indicating no days are selected.
[Flags] enum DaysOfWeek {
None = 0,
Monday = 1,
Tuesday = 2,
Wednesday = 4,
Thursday = 8,
Friday = 16,
Saturday = 32,
Sunday = 64
};
Combining Bitwise Enums
To combine the Monday, Wednesday, and Friday enum values we use the bitwise OR (|) operator.
DaysOfWeek selectedDays = DaysOfWeek.Monday | DaysOfWeek.Wednesday | DaysOfWeek.Friday
Checking for Enum Values
To check if the Monday value is set in the selectedDays variable we use the bitwise AND (&) operator.
if ((selectedDays & DaysOfWeek.Monday) != 0) {
Console.WriteLine("Monday is selected.");
}
Removing Enum Values
To remove the Wednesday specific enum value from the set in the selectedDays variable, we use the bitwise XOR (^) operator.
selectedDays ^= DaysOfWeek.Wednesday;
VB.Net - Syntax
Enum enShapes 'As integer - by default
Triangle ' This is assumed to be zero
Square '1
Rectangle '2
Circle ' 3
Unknown = -999
End Enum
Dim Shape As enShapes = enShapes.Triangle
System.Windows.Forms.MessageBox.Show([System.Enum].GetName(GetType(EN_SHAPES,2); // = Green
Even though Enum values are internally stored as integers, by default you are not allowed to assign a number to an Enum variable if Option Strict is on.
You must convert the number to the proper Enum type before assigning it an Enum variable.
Shape = CType(1, enShapes)
© 2026 Better Solutions Limited. All Rights Reserved. © 2026 Better Solutions Limited TopPrevNext