Chapter 15: Enums and Structs

1. Enums – Named Constants (Super Clean & Readable)

Enums (short for enumerations) let you define a set of named constants that represent a group of related values.

Real-life analogy: Think of days of the week: Monday, Tuesday, Wednesday… Instead of using magic numbers (1 = Monday, 2 = Tuesday), you use meaningful names!

Basic Syntax:

C#

Usage:

C#

Custom Values (you can assign your own numbers):

C#

2. Enums with [Flags] – Represent Multiple Choices

Sometimes you want one variable to hold multiple options at the same time (like permissions, days available, features enabled).

You use the [Flags] attribute + powers of 2 values.

C#

How to combine multiple flags:

C#

Beautiful display (with ToString):

C#

3. Structs – Value Types (Like Lightweight Classes)

Structs are similar to classes, but they are value types (stored on the stack, copied by value).

When to use structs?

  • Small, simple data containers
  • Performance-critical code (games, math, coordinates)
  • You want copy-by-value behavior (not reference)

Syntax:

C#

Usage:

C#

4. Structs vs Classes – The Big Differences

Feature Struct (Value Type) Class (Reference Type)
Stored where? Stack (or inline in other objects) Heap
Assignment behavior Copies the entire value Copies the reference (same object)
Default value All fields zeroed (0, false, null…) null
Inheritance Cannot inherit from other structs/classes (only interfaces) Full inheritance possible
Performance Faster for small data (no GC pressure) Slower for small objects (heap + GC)
Best for Small, immutable data (Point, DateTime, Color) Complex objects (Person, Order, Car)
Can be null? No (unless Nullable<T> or T?) Yes
Boxing/unboxing Yes (when cast to object/interface) No

Real example – Point as struct vs class

C#

Mini-Project: Card Game with Enums & Structs

C#

Summary – What We Learned Today

  • Enums → named constants (days, error codes, ranks…)
  • [Flags] enums → combine multiple values with and check with &
  • Structs → lightweight value types (stack, copied by value)
  • Struct vs Class → use struct for small, immutable data; class for complex objects

Your Homework (Super Fun & Practical!)

  1. Create a new console project called EnumsAndStructs
  2. Create:
    • Enum UserRole with flags: None, Viewer, Editor, Admin, SuperAdmin
    • Struct GamePosition with X, Y, Z (float) and method DistanceTo(GamePosition other)
    • Struct PlayingCard with Suit (flags enum) and Rank (enum)
  3. In Program.cs:
    • Create a user with multiple roles (e.g., Editor + Admin)
    • Check if they have Admin rights
    • Create two cards, compare suits, check if red
    • Create two GamePositions and calculate distance

Next lesson: Exception Handling – we’re going to make your programs bulletproof against crashes and errors!

You’re doing absolutely fantastic! 🎉 Any part confusing? Want more examples with flags or structs? Just tell me — I’m right here for you! 💙

You may also like...

Leave a Reply

Your email address will not be published. Required fields are marked *