C #: from the basics to the medium level [Reference type vs Value type (Part I)]

Matteo Lo Piccolo
3 min readJul 22, 2021

Ok, we are going really good.
But now we have to talk about one of the most important topic in programming : reference type and value type.

First of all : what are these “type”?
When we talk about reference type and value type, we talk about memory management. Even if C# has the Garbage Collector (in C++ we have to manage ALL), that’s don’t mean we can simply create a billion of object and everything works fine. Absolutely not! But the Garbage Collector, if we make a good work, help us a lot.
But tha fact remain : we need to understand these type of things.

Now, there are primitive types and not-primitive types.
Primitive types are int, char, float, bool
Non-primitive types are classes, structs, arrays, strings
Now, remember that both Arrays and Strings are classes (System.Array and System.String).

All this to explain an important concept:
in C # we have two main types from which we can create a new type

We have Classes and Struct.

In Structs we have primitive types and custom structs
In Classes we have non-primitive types and custom classes

As I can said before, the main difference is about memory management, and is important understand what happen “behind the scene” when we use one type or other type, and if our program work weird, we can solve this type problems.

Ok, so we now know :
Structs are Value types and Classes are Reference type.

When we create a value variable type, it allocated in STACK MEMORY.
This type of allocation is automatically, so we don’t have to worry about it.
Every time this variables goes ot of scope, it IMMEDIATLY REMOVE FROM THE STACK, by Runtime or CLR.

With Reference type, we, programmers, HAVE TO ALLOCATED IT IN THE MEMORY.
Remember when we use the new keyword?
Every time we use new we told the runtime to allocated memory for this object.
And the place in memory where its allocated is the HEAP.
This type of object, when goes out of scope, is not destroyed immediately, but living in memory for a while.
And here comes the Garbage Collector, which is done again by Runtime or CLR and clean up for us.

What we really need to understand is what happens when we copy an object, and how the two different types behave.
Let’s look what’s happen in code.

We start with Reference types.

I create a int variable, and we try to copy its value on another variable

after that, we try to increment the value of b

Now, what you think the value of a ?
Let’s look at it

a is 10, b is 11.

P.S. : you can use the string.Format, or you can type this and it works in any case

This happen because int is a value type, so when you copy a value type, it take that value, and store it into target location in memory.

I try to explain better : when we copy a value type on another, in the Stack memory we have
a = 10.
after copy, we have
a = 10 and b = 10
so if we add something to a or b, they work completely independently of each other.

--

--

Matteo Lo Piccolo

Always in love with programming, even if late (I'm already 39 years old) I decided to follow my dream! We will see how far my passion will take me!