A value type is a type that when declared as a variable, allocates some data at a memory address. A reference type is similar, but as its name suggests the variable in this case would instead hold a reference (memory address) to another part of memory where the memory for the data actually lives.
The reason for the distinction is that a value type data will have some deterministic size & all of the memory for its member variables will be allocated up front (even if it's filled with default (zero) values). You can't have a "null" value type. "null" is only used to signify a missing memory address (reference).
You can have a reference to a value type, though. You just need to use one of the ref
, out
or in
keywords. Not using one of these keywords would mean that creating a new variable and assigning it the value of an existing value type (including struct) would result in a copy of all the variables data. It's important to consider when using ref
for a value type makes sense. For example, a 2-component vector containing 2 32-bit floats for a 64-bit process should avoid ref
and just allow the copy, as the memory overhead is the same, but using ref
creates the overhead of indirection.
Just some extra bits of information: https://learn.microsoft.com/en-us/dotnet/csharp/language-reference/keywords/reference-types