Values versus References
There are different ways for a compiler to talk about data, and C# has two different ways
to do so. All datatypes in C# fall into one of two categories:
_ Value types
_ Reference types
I’ll explain each of these different kinds of types in the following sections.
Value Types
A value type is typically a small piece of data that the system spends very little time managing.
You have already used value types in Chapter 2 with all of the built-in numeric data
types. —such as ints, floats, and so on—is a value type.
note
Value types are created on the system stack. You don’t necessarily need to know what that is, but
if you’re interested, I strongly urge you to research it on your own. This topic goes beyond the scope
of this book, so I don’t have enough room to explain it here, but it greatly helps you understand
exactly how computers work, which will in turn make your programs faster and more efficient.
Value types are fairly simple and straightforward to use, as you can see in this code:
int x = 10, y = 20;
x = y; // value of y is copied into x
y = 10; // y is set to 10
Along with the built-in data types, structures are value types as well, which I explore in
much more detail later in this chapter.
Reference Types
Reference types are completely different from value types. Classes, unlike structures, are
always reference types. Reference types, rather than storing the data directly, store an
address inside of them, and that address points to the actual data in the computer somewhere.
Declaring a Reference Type