Unity and C# Basics — Variables
Variables or Types allow us to store values that are used and potentially changed during the course of our program. They are essentially our building blocks or information containers.
In C# there are two main Types:
- Value Types
- Reference Types
Value Types are variables that are stored in a pre-defined location in memory — specifically on the Stack. These Types hold their data inside their memory allocation. This allocation is static and its managed efficiently by the CPU. They include but are not limited to the following:
Reference Types are stored and handled differently than Value Types. Reference Types, like their name suggests, hold a reference which points to their value that is stored on the Heap. Reference Types have a default value of null
. Some examples are:
To define a variable in C# we can use the following syntax: