Game Development Reference
In-Depth Information
Figure 1.2
Value and reference types in memory.
reference type is more like adding a note to the shelf that gives directions to a
different part of the library where the topics are stored.
Here's an example of a value type.
int i
¼
100;
If we looked in memory to see where
i
was stored, we'd get the number 100. In
our human-readable code we use the variable name
i
; in assembly code
i
is a
memory address. This line of code, therefore, says put the value
100
in the next
available memory address and call that memory address
i
.
int i
¼
100;
int j
¼
i; // value type so copied into i
Console.WriteLine(i); // 100
Console.WriteLine(j); // 100
i
¼
30;
Console.WriteLine(i); // 30
Console.WriteLine(j); // 100
In this example, the value type
i
is assigned to
j
, which means its data is copied.
Everything stored in memory address
j
is now the same as whatever was in
i
.
The next example is similar but it uses reference types. First, a reference type
needs to be created by defining a new class. All this class will do is store a number,
like our own version of the
int
type. Our type will be called
Int
using a capital I
to differentiate it from C#'s built-in
int
type.
class Int()
{
Search WWH ::
Custom Search