When to Use Struct vs. Class

The key difference is that a class is a reference type and a struct is a value type. Use a struct for small, simple data structures that represent a single value (like Point or Color) and have a size under 16 bytes. For everything else, especially types with complex behavior or larger data, a class is the better choice.

C# Example Code
using System;

// A struct is a value type - good for small, immutable data.
public struct Point
{
    public int X { get; }
    public int Y { get; }

    public Point(int x, int y)
    {
        X = x;
        Y = y;
    }
}

// A class is a reference type - good for more complex objects.
public class Rectangle
{
    public Point TopLeft { get; set; }
    public int Width { get; set; }
    public int Height { get; set; }
}

// Structs are copied on assignment
Point p1 = new Point(10, 20);
Point p2 = p1; // p2 is a copy of p1
// Modifying p1 does not affect p2 (if it were mutable)

// Classes hold references
Rectangle r1 = new Rectangle { Width = 100 };
Rectangle r2 = r1; // r2 refers to the SAME object as r1
r2.Width = 200;

Console.WriteLine($"p1.X: {p1.X}"); // 10
Console.WriteLine($"r1.Width: {r1.Width}"); // 200