Learn C# in Y Minutes
C# 14 · .NET 10

06. Structs & Enums

Classes are not the only way to make your own types. A struct is a lightweight type for small values, and an enum gives names to a fixed set of options.

Structs

A struct looks almost like a class: fields, methods, new. The big difference is how it is copied. Assigning a struct to another variable copies all its data, so the two variables are independent. Changing orange leaves red untouched.

app/06-structs-enums/Struct.cs
var red = new RgbColor { R = 255, G = 0, B = 0 };
var orange = red;
orange.G = 165;

Console.WriteLine($"red:    {red.ToHex()}");
Console.WriteLine($"orange: {orange.ToHex()}");

struct RgbColor
{
    public byte R;
    public byte G;
    public byte B;

    public string ToHex() => $"#{R:X2}{G:X2}{B:X2}";
}
red:    #FF0000
orange: #FFA500

The { R = 255, ... } part is an object initializer: it sets fields right after creation. It works for classes too.

Struct or class?

Default to a class. Reach for a struct only when all of these are true:

  • It represents a single small value, like a color, a point, or a money amount.
  • It holds only a few fields (roughly 16 bytes or less).
  • Its value rarely changes after creation.

Many built-in types are structs for exactly these reasons: int, bool, decimal, and DateTime. Lesson 07 shows why this copying behavior matters.

Enums

An enum is a set of named constants. Instead of passing a mysterious 2 around, you write ShippingMethod.Express. The compiler catches typos, and your editor autocompletes the options. Under the hood each name is an int, and you can cast back and forth.

app/06-structs-enums/Enum.cs
var method = ShippingMethod.Express;

Console.WriteLine(method);
Console.WriteLine((int)method);

var fromNumber = (ShippingMethod)3;
Console.WriteLine(fromNumber);
Console.WriteLine(method == ShippingMethod.Express);

enum ShippingMethod
{
    Standard = 1,
    Express = 2,
    Overnight = 3
}
Express
2
Overnight
True

Give enum members explicit values when they are stored in a database or sent over the network. Otherwise, inserting a new member in the middle silently renumbers the rest.

Parsing enums

Text often needs to become an enum, for example a value typed by a user. Enum.Parse throws on bad input; Enum.TryParse is the safe version, just like int.TryParse. Pass ignoreCase: true to accept express as well as Express.

app/06-structs-enums/EnumParse.cs
var chosen = Enum.Parse<ShippingMethod>("Overnight");
Console.WriteLine($"Parsed: {chosen} ({(int)chosen})");

string[] inputs = ["Express", "express", "Teleport"];

foreach (var input in inputs)
{
    if (Enum.TryParse<ShippingMethod>(input, ignoreCase: true, out var method))
        Console.WriteLine($"\"{input}\" -> {method}");
    else
        Console.WriteLine($"\"{input}\" -> unknown method");
}

enum ShippingMethod
{
    Standard = 1,
    Express = 2,
    Overnight = 3
}
Parsed: Overnight (3)
"Express" -> Express
"express" -> Express
"Teleport" -> unknown method

Enums are perfect for fields like order status or user role. By default, ASP.NET Core sends enums in JSON as numbers ("status": 2). Register JsonStringEnumConverter to send readable names instead.

Program.cs
builder.Services.ConfigureHttpJsonOptions(options =>
    options.SerializerOptions.Converters.Add(new JsonStringEnumConverter()));

Quick quiz