0Pricing
C# Academy · Lesson

Exploring Enums and Structs

Discover enums and structs to represent fixed sets of values and lightweight objects.

Exploring Enums and Structs is a free C# Academy lesson on CoddyKit — lesson 4 of 6. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the C# Academy learning path, one of 6 lessons in the course, and your progress syncs across the web and the CoddyKit app.

1

Exploring Enums and Structs

Welcome to the next lesson! In this lesson, you’ll learn about enums and structs in C#, their purposes, and how to use them to organize your code and manage data effectively. Let’s get started!

Exploring Enums and Structs — illustration 1

2

What is an Enum?

An enum (short for enumeration) is a value type that defines a set of named constants. It is used to make code more readable and manage related values.

Example: Days of the week, months, or status codes can be represented as enums.

3

What is an Enum?

An enum (short for enumeration) is a value type that defines a set of named constants. It is used to make code more readable and manage related values.

Example: Days of the week, months, or status codes can be represented as enums.

Key Point: Enums provide meaningful names for numeric values, improving code clarity.

<h2>Defining and Using Enums</h2>
<p>To define an enum, use the <code>enum</code> keyword. Example:</p>

4

What is a Struct?

A struct is a value type that groups related variables into a single type. It is used for lightweight objects that do not require inheritance.

Example: Representing a point in a 2D space with X and Y coordinates.

5

Defining and Using Structs

To define a struct, use the struct keyword. Example:

Tip: Use structs for small, immutable data types that represent a single value or concept.

using System;

struct Point {
    public int X;
    public int Y;

    public void Display() {
        Console.WriteLine($"Point: ({X}, {Y})");
    }
}

class Program {
    static void Main() {
        Point point = new Point { X = 10, Y = 20 };
        point.Display(); // Outputs: Point: (10, 20)
    }
}

6

Enums with Explicit Values

You can assign specific values to enum members. Example:

Key Point: Explicit values make enums useful for mapping to constants like HTTP status codes.

using System;

enum HttpStatusCode {
    OK = 200,
    BadRequest = 400,
    NotFound = 404,
    InternalServerError = 500
}

class Program {
    static void Main() {
        HttpStatusCode code = HttpStatusCode.OK;
        Console.WriteLine($"Code: {(int)code}, Status: {code}"); // Outputs: Code: 200, Status: OK
    }
}

7

Practice Challenge

Create an enum OrderStatus with values Pending, Processing, Shipped, and Delivered. Create a struct Order that includes an ID and status. Example:

using System;

enum OrderStatus {
    Pending,
    Processing,
    Shipped,
    Delivered
}

struct Order {
    public int ID;
    public OrderStatus Status;

    public void Display() {
        Console.WriteLine($"Order ID: {ID}, Status: {Status}");
    }
}

class Program {
    static void Main() {
        Order order = new Order { ID = 1, Status = OrderStatus.Processing };
        order.Display(); // Outputs: Order ID: 1, Status: Processing
    }
}

8

9

Best Practices for Enums and Structs

Here are some best practices:

  • Use enums for related constants that improve code readability.
  • Use structs for lightweight, immutable objects with no inheritance.
  • Avoid adding complex methods or logic to structs to maintain their simplicity.

Tip: Combine enums and structs to represent real-world entities effectively.

10

Great Job!

Congratulations! You’ve learned how to use enums and structs to organize and manage data effectively in C#. In the next lesson, we’ll explore delegates and events to handle asynchronous and event-driven programming. Let’s keep coding!

Exploring Enums and Structs — illustration 10

Frequently asked questions

Is the “Exploring Enums and Structs” lesson free?

Yes — the full text of “Exploring Enums and Structs” is free to read here on the web, and the C# Academy course includes 6 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the C# Academy course, upgrade to CoddyKit PRO.

What will I learn in “Exploring Enums and Structs”?

Discover enums and structs to represent fixed sets of values and lightweight objects. You practise C# Academy with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.

Do I need any experience to start C# Academy?

No prior experience is required. C# Academy on CoddyKit is structured for beginners through advanced learners; this is — lesson 4 of 6, so you can start here or from the beginning and move at your own pace.

How long does the “Exploring Enums and Structs” lesson take?

Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.

Can I write and run code in this C# Academy lesson?

Yes. Every C# Academy lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.

All lessons in this course

  1. Introduction to Interfaces
  2. Abstract Classes
  3. Static Members and Methods
  4. Exploring Enums and Structs
  5. Delegates and Events
  6. Working with Collections: Lists, Dictionaries, and Queues
← Back to C# Academy