What Is an Enum in Programming? Names, Numbers and Game Code
What is an enum in programming? An enum is a type that gives names to a short, fixed list of values. You write the list once, and from then on a variable of that type can only hold one of those names. Behind each name, the computer stores a small number. This guide shows you both sides, using real enums from the code of a small browser tank game.
What is an enum in programming? The short answer
"Enum" is short for enumerated type. Wikipedia describes it as a data type made of a set of named values. A variable of that type can be given any one of those values.
Here is one from Tank City Reboot, written in C#:
enum Kind { Player, Worm, Trojan, Spyware, Rootkit }
That line creates a new type called Kind. A tank's kind can be Kind.Worm or Kind.Rootkit. The code reads like the game: your tank is the player, and the other four are malware tanks.
You already know one enum. Wikipedia notes that the true or false type is often a built-in list of two values: false and true.
Why programmers name values instead of using bare numbers
Without an enum, you could store a tank's kind as a plain number. 0 is the player, 1 is a worm, and so on. That works, but the meaning lives only in your head.
Read if (k == 3) a month later and you have to hunt for what 3 was. Read if (k == Kind.Spyware) and you know at once.
The declaration also becomes the full list of choices, in one place.
What the computer stores: names on the outside, numbers inside
Microsoft's C# reference says an enum is a set of named constants of an underlying whole-number type. By default that type is int. The values start at zero and go up by one, in the order you write the names.

Four enums from the game's code. Each name sits on a number that starts at 0 and goes up by one.
So in Kind, Player is 0, Worm is 1, Trojan is 2, Spyware is 3 and Rootkit is 4. Those numbers are just patterns of bits, the 0s and 1s a computer holds.
You can also pick the storage type. The game's map uses this one:
enum Bit : byte { Empty, Brick, Steel, Water, Fog, Ice, Base }
The : byte part tells C# to store each value in one byte instead of a four-byte int. The map is a 52 by 52 grid of these, so the whole tile map takes 2,704 bytes. Each cell covers 4 by 4 pixels on screen.
Microsoft's page also shows that you can set the numbers yourself, like None = 0, Unknown = 1, ConnectionLost = 100. You might do that when the numbers must match something outside your code.
A worked example: the stage clear tally
When you clear a stage, the game shows how many of each malware tank you destroyed and what they were worth. The code behind that screen is a good enum example to follow by hand.
The game keeps a list of names that lines up with the enum numbers:
KindNames = { "", "WORM", "TROJAN", "SPYWARE", "ROOTKIT" }
Slot 0 is empty because 0 is the player, who never appears on the tally. Then a loop counts k from 1 to 4 and turns each number back into a kind with a cast, (Kind)k.
Follow one pass yourself. Say you destroyed two spyware tanks.
- The loop reaches
k = 3. (Kind)3isKind.Spyware, because Spyware is the fourth name and counting starts at 0.KindNames[3]is"SPYWARE", so that is the label printed on the row.Points(Kind.Spyware)returns 300. The same function gives a worm 100, a trojan 200 and a rootkit 400.- The row reads
2 * 300 = 600, and 600 is added to the tally's total.
Now try k = 1 with three worms. You should get WORM and 3 * 100 = 300. If you can do that in your head, you understand how an enum turns names into numbers and back again.
Enums in games: how Tank City Reboot uses them
Games are full of short, fixed lists, so they are a clear place to watch enums work. The first game's code has four of them.
- Kind picks each tank's color and shape. One function in Art.cs switches on the kind to choose a color.
- Power lists the six power-ups: Patch, Overclock, Breakpoint, Garbage, Firewall and Backup.
- State lists the screens: Title, Card, Play, Clear and Over. The main loop runs a
switchon it, so each screen has its own block of code. - Bit marks what fills each map cell. When the game draws the board, it switches on the Bit under each pixel and paints brick, steel, water, ice or fog.
The second game, Tank City Zero Day, reuses the same Bit enum. Its own Kind has seven members: Player, Bot, Worm, Wormlet, Ransom, Miner and Boss. Its State swaps Clear for Upgrade, and a new enum called Up lists ten upgrades, from Multithreading to HotPatch.
The game teaches the idea behind each name, such as what a rootkit or a cache is. It is not a course, but reading its enums is a quick way to see the whole cast of a game in one line.
Common enum mistakes to watch for
Enums are simple, but the number underneath can still trip you up. These points come from Microsoft's C# reference.
- A cast does not check the list. In C#,
(Season)4compiles even when no member has the value 4. It just prints 4. UseEnum.IsDefinedwhen a number comes from outside your code. - Zero is always allowed. C# lets the literal 0 turn into any enum type. If your enum has no member worth 0, you can end up holding a value with no name.
- Order is meaning. Because values count up in the order you write them, moving a name changes its number. The tally above depends on Worm being 1 and Rootkit being 4.
- Combinations need flags. If one value must hold several choices at once, give each member a power of two. Then join them with
|and test them with&, and mark the enum with theFlagsattribute.
The game's code has safe defaults of its own. Points returns 0 for any kind it does not list, and the map reads Steel for any cell outside its edge.
Frequently asked questions
Is an enum a data type or a variable?
An enum is a data type. You declare it once, then make variables of that type, and each variable holds one of its members.
What number does the first enum member get?
In C#, the first member is 0 unless you set it yourself, and each next member is one more. So in Kind, Player is 0 and Rootkit is 4.
Can I turn a number back into an enum?
Yes. In C# you cast it, as the game does with (Kind)k. The cast does not check that a member exists, so check numbers that come from outside your code.
Why use an enum with a switch statement?
The enum gives you the full list of cases by name. A switch on it, like the game's switch on State, keeps each case's code in its own clear block.
Get started
Play a stage, clear it, and watch the tally rows count up. Each row is one pass of the loop you followed above. Then open Zero Day and spot the new kinds of malware tanks its longer Kind list adds.
Play Tank City Reboot: it is free, plays in your browser on a computer or a phone, and playing never needs an account.
Comments
No comments yet.
Sign in or make an account to comment.