In a previous article we talked about the basics of classes in C# and Unity. However I intentionally left out Structs because you can’t really talk about Structs without talking about Memory Management in Unity. So what are Structs exactly? In a nutshell they’re basically a class but without the ability to use inheritance. This makes them “faster” because they’re more lightweight. The tricky part of their value refers to how memory management works.

The Stack and The Heap

In memory management you have to main concepts, the stack and the heap. They deal with the types of memory, value types and reference types. Structs are a value type and those go on the Stack, it’s very orderly with Last In First Out (LIFO) organization, and cleaning up after itself in memory. Other kinds of value types would be things like int, long, double, bool, float, struct, char, enum, and etc. It is important to remember that value types are the actual data of the variable this will be more apparent when I explain reference types.

By contrast the Heap is a collection of reference types that can come and go in any order they please.Reference types aren’t the actual data but just pointer to the actual data. If you change a reference type that doesn’t actually change the underlying value. The Heap is really designed for data that will exist beyond the life of a single method. Since data can exist for much longer than than the class/method that created it there is no automatic clean up of that allocated memory when done. As a result C# runs a “garbage collector” to periodically check that the data is no longer in use. This is not very performant for a few reasons but that’s the main kicker of using a Struct. It typically doesn’t trigger the garbage collector since it cleans up after itself.

So When Do I Use What?

So when should you use a struct over a class? Using structs is often considered an optimization tool but knowing when and where to use them is more important and should only be considered as a side-benefit of making your code more readable/modular. There are no hard and fast rules but some things to keep in mind when using Structs vs Classes. Typically structs only have 4 or so variables, if you have less than 5 then it should be a struct. If you have more than 5 then a class may be a better fit. If you want a default constructor without parameters then you want a class otherwise a struct is your ticket. If you need to access the value of a variable directly instead of a reference to the value then you only do that with structs. They work best with data that won’t change often (immutable) like weapon stats, enemy stats, and other mostly static pieces of data. When you pass around data in a struct it doesn’t really change but if you need data change then using reference types like a class are your ticket.

Structs and memory management in Unity isn’t tough grasp but considered one of the essential topics to understand if you’re interested in expanding your professional experience in the industry. Almost every company asks for an explanation of these types or expects you to know from jump. Hopefully this gave you a top-level understanding of what a struct is, how and when to use it, and why you should care. Until next time, happy coding.