C# Value type vs Reference type

Good morning everyone.

Today I’m going to discuss the difference between value data types and reference data types.

Value Types

A value data type is a variable that stores its own data. An example of a value data type is an integer. Given the code example below

int number = 5;

The variable number is storing the value of 5.

Reference Types

A reference data type doesn’t store the value of that data, it actually stores the reference to the data instead.

It doesn’t tell the compiler what the value of the data is, but where to find the data.

Let’s look at the example below

string message = "Hi"

The variable message does not actually store the value "Hi" . "Hi" is created elsewhere in the computer’s memory. message stores the address of that memory location.

This is a very important concept to understand because when we start getting into topics such as pass by value and pass by reference, it will affect the way our code is written and outputs.

Pass by value and Pass by reference will be discussed in an upcoming post.