Structs

struct  vs class

  • class

    • reference type

    • allocated on heap

    • managed by GC

  • struct

    • value type

    • usually stack allocated

    • copied by value

struct Vec2 {
    public float x;
    public float y;
}

readonly struct

  • Prevents mutation after creation.

  • Benefits:

    • safer value semantics

    • avoids defensive copies

readonly struct Vec2 {
    public readonly float x;
    public readonly float y;
}