Value Types and Structs!

Objective: understanding what a value type and a struct are and how they work.

What is a struct? Structs are often referred to as performance enhancements and replacements to classes. It is a nano-performance and should not be looked at as a way to increase performance. A rule of thumb for structs is to use if you have less than 4 fields, and you don’t need the use of inheritance.

Structs have many similarities to a class. instead of the class keyword you would use struct.

inside you can add fields and variables.

We are just defining what the traits and blueprint of an item is. You can also have a constructor in a struct.

The main difference between struct and class is that class supports inheritance. You cannot make subclasses with structs. Ideally your structs should not need to change, and they don’t have/need the ability for inheritance. What is important to understand is the type of information that a struct is and how it is treated, otherwise known as memory management.

A struct is considered a value type. This is important because in your projects you will be passing around information and it is important to understand how value types are passed around.

In C# we don’t need to worry about memory management much because memory management is built into the programming language. We have what is called Garbage Collecting.

What is a value type? Value types are stored on what is known as the stack. What is important is how the data is stored how the data is passed around.

Here is an example of how a value type is stored.

A data type is a value type. If it holds a data value within its own memory space. This means variables of these data types directly contain their values for example age is 25, it’s a value type.

Some other examples of data type values or some value type data types are bool, bytes, char, doubles, float, int, and long. Anything that has associated value as well as structs. Structs are value types!

Now that we know what a value type is let’s look at how the data is passed around. When you pass a value text, a value type, if you pass it into a function it would clone or “copy” this value however, you could not change the original value.

This is an example of how to initialize items from a struct.

You’ll notice the struct did not need to use the new operator. You don’t have to create an instance of an object; it automatically exists because it’s a value type.

A key deciding factor on if you should use a struct or a class is, are you going to be passing that class around to other methods. Understand that if you make modifications within that method it’s going to be ignored.

The breads name before method is the same after. This is because this is a value type. It basically copied that data and created its own version because it is a value type you can only modify original copies of reference types.

If you Debug.Log it will print out dirty bread.

This is a basic rundown of structs and value types. Next time we will explore classes and reference types! As always, don’t be afraid to experiment with your code, and I’ll see you in the next tutorial!