Level 4 — Classes
Enums
Original notes by SunDiaL · View English source
An enum is the simplest form of custom operands — a way to group and name related constants as a value type.
You can achieve the same thing with a static class with const fields, but an enum has the added benefit of being type safe.
Defining an enum
public enum Day
{
Monday;
Tuesday;
Wednesday;
Thursday;
Friday;
Saturday;
Sunday;
}
Behind the scenes, enums are encoded as ints or bytes, starting from 0 and counting up (i.e. Monday = 0, Tuesday = 1). But this can be explicitly set by the programmer.
public enum Day
{
Monday = 1;
Tuesday = 2;
Wednesday = 3;
Thursday = 4;
Friday = 5;
Saturday = 6;
Sunday = 0;
}
Using an enum
As with any type, you use enums to create variables.
Day x = Day.Monday;