Introduction

After tackling the complexity of arrays and memory management, Enumerations—or Enums for short will feel like a breath of fresh air. While they are technically simple to write, they are one of the most powerful tools for making your code readable, professional, and “bug-proof.”

The word enumerate literally means “to name or count off one by one.” In programming, an Enum allows you to create a custom set of named constants. Instead of using confusing numbers or strings to represent states, you create a dedicated “menu” of options that the computer understands perfectly.

The Problem: The “Magic Number” Trap Imagine you are coding a Player character. You might use an integer to keep track of their current action:

int playerState = 1; 

if (playerState == 1) {
    // Does 1 mean "Idle"? "Running"? "Jumping"? 
    // If you forget, your game logic breaks.
}

This is called a Magic Number. It’s hard to read, easy to mess up, and a nightmare to debug when your game gets bigger.

The Solutions: The Enum

1. Defining Your own Type When you create an enum you are literally creating a new Data Type, just like int or string Because this is a high-level definition, you don’t’ put it inside your logic ( like inside a loop), you place it at the bottom of your file or outside your main logic block.

syntax

[Access Modifier] enum [Name] { [Values] }

/*default access modifier : internal*/
// Top of your file: Your custom types
public enum PlayerState 
{ 
    Idle, 
    Running, 
    Jumping, 
    Falling, 
    Dying 
}

// Bottom of your file: Your game logic
Console.WriteLine("Game Starting...");

public Access Modifier. it tells the computer who is allowed to see and use your custom type.

1. Why we use public In game development, you rarely have just one file. You might have one script for the Player, one for the Enemy, and one for the UI. If you define your PlayerState at the bottom of Program.cs without the word public, other parts of your game might not be able to “see” it. Using public makes the Enum a global standardthat every script in your project can understand.

2.Declaring and Assigning State Once defined, you use PlayerState just like you would use int However, instead of assigning a number or text. you access the specific values using the Dot Notation (Type.Value)

// 1. Declare the variable
PlayerState currentState;

// 2. Assign a value using the dot notation
currentState = PlayerState.Idle;

// Shorthand version:
PlayerState nextAction = PlayerState.Jumping;

3.Comparing States (Logic) Enums are perfect for if statements and switch cases. They allow the computer to check equality very quickly while keeping the code readable for you.

PlayerState currentState = PlayerState.Jumping;

if (currentState == PlayerState.Jumping || currentState == PlayerState.Falling)
{
    Console.WriteLine("Player is in the air!");
}
else if (currentState == PlayerState.Dying)
{
    Console.WriteLine("Trigger Game Over screen.");
}
else
{
    // The default state
    Console.WriteLine("Player is currently: " + currentState);
}

Wait, what happens in that last line? > When you use + currentState, C# automatically converts the enum value into a string. So PlayerState.Idle literally prints the word “Idle”.

4.Understanding the order Even though we see words, the computer sees a list. The order you write them in matters if you use comparison operators (< or >).

enum PlayerState { Idle, Running, Jumping }

// Idle is 0
// Running is 1
// Jumping is 2

PlayerState currentState = PlayerState.Running;

if (currentState > PlayerState.Idle) 
{
    Console.WriteLine("The player is moving!"); 
}

currentState vs PlayerState

// 1. We define the CATEGORY (The Menu)
enum PlayerState { Idle, Running, Jumping }

// 2. We create a SLOT for that category (The Order)
PlayerState currentState = PlayerState.Idle; 

// 3. We check the SLOT against the CATEGORY
if (currentState == PlayerState.Running) 
{
    // "Is the choice inside the box EQUAL TO the Running label?"
}

Why “PlayerState.Running” instead of just “Running”?

  1. PlayerState (Idle, Running, Jumping)
  2. EnemyType (Running, Flying, Boss)

If you just typed if (currentState == Running), the computer wouldn’t know if you meant the Player running or the Enemy running! By typing PlayerState.Running, you are being specific: “The Running state from the Player category.”

Setting a custom Staring point If you want your PlayerState to start at 20 instead of 0, you simply assign it in the definition.

enum PlayerState 
{ 
    Idle = 20,    // We force this to be 20
    Running,      // The computer now assigns this 21
    Jumping,      // This becomes 22
    Falling       // This becomes 23
}

Assigning Every value Manually You aren’t restricted to counting in order. You can give every single entry its own unique ID. This is very common for Item Databases or Network Codes.

enum GameEvent
{
    PlayerJoined = 100,
    PlayerLeft = 101,
    CapturePoint = 200,
    MatchEnd = 999
}

converting between Enum and Int (casting) Because they are technically integers, you can “force” them to switch back and forth. In programming, we call this Casting.

From Enum to Integer:

If you want to know the number behind the name:

PlayerState myState = PlayerState.Jumping;
int stateNumber = (int)myState; 

Console.WriteLine(stateNumber); // Output: 2

From Integer to Enum:

If you receive a number (maybe from a saved game file) and want to turn it into a state:

int savedData = 1;
PlayerState loadedState = (PlayerState)savedData;

Console.WriteLine(loadedState); // Output: Running

Why Enums are Vital for Game Dev

  1. Readability: Your code explains itself. Anyone reading your script knows exactly what PlayerState.Dying means without looking at a manual.
  2. Type Safety: If a variable is a PlayerState, you cannot accidentally set it to 100 or "Flying" if those aren’t in your list. The compiler will stop you before the game even runs.
  3. Efficiency (IntelliSense): In your IDE (like Visual Studio), as soon as you type PlayerState., a list of all your options pops up. This makes coding much faster.

In game development, Enums and Switch statements are best friends. If an Enum is the “Menu” of states, the Switch statement is the “Machine” that decides what code to run based on the current State.

Here is how to combine them to create a simple Game State system.

1. Using Switch Statements with Enums A switch statement is much cleaner than writing five different if/else if blocks. It looks at the Enum variable once and jumps straight to the correct “case.”

 enum PlayerState 
{ 
    Idle ,    
    Running,    
    Jumping,    
    Falling   
}

PlayerState currentState = PlayerState.Idle;

switch (currentState)
{
    case PlayerState.Idle:
        Console.WriteLine("Player is breathing calmly.");
        break;
        
    case PlayerState.Running:
        Console.WriteLine("Playing footsteps sound effect.");
        break;
        
    case PlayerState.Jumping:
    case PlayerState.Falling:
        // We can stack cases! Both Jumping and Falling do this:
        Console.WriteLine("Player is in the air.");
        break;

    default:
        Console.WriteLine("State: " + currentState);
        break;
}

2. converting input to Enum (The switch Expression) Sometimes, you need to turn a string (like a command typed by a player) into an Enum. C# has a modern, shortcut way to do this called a Switch Expression.

Imagine a debug console where you type the state you want the player to switch to:

Console.Write("Enter new state (Idle, Running, Jumping): ");
string input = Console.ReadLine().ToLower();

// The Switch Expression "maps" the string to the Enum
PlayerState selectedState = input switch
{
    "Idle"    => PlayerState.Idle,
    "Running" => PlayerState.Running,
    "Jumping" => PlayerState.Jumping,
    _         => PlayerState.Idle // The "_" is the "Default" (handles mistakes)
};

Console.WriteLine("State updated to: " + selectedState);