‫Welcome back.
‫In this video, we are going to look at the difference between value type and reference type.
‫So the data types can be categorized into two categories based on how they occupy memory location.
‫And we will look into those types in detail.
‫So first of all, we look at the value type.
‫So as the name suggests, it is a data type which when declared and assigned a value, holds that value
‫directly on its own memory space.
‫All right.
‫So the value type uses a heap to store the value example of value types are data types such as int float
‫long double char bool decimal double.
‫So basically the simple data types right now let's look at the pictorial representation of a value type.
‫So let's say we create a new variable called Z and it has the value of 234.
‫So now this value 234 will be stored in RAM at the position of this Z variable.
‫All right.
‫And then if we look at the reference type in more detail, we can see that it is a variable type which
‫instead of storing the value in memory directly, stores only the memory location of actual data.
‫The variable here stores the memory reference of the data and not the data directly.
‫This is the huge difference.
‫Right?
‫So reference data types are string class array, basically data types that are more complex and need
‫a lot more space, so to speak.
‫This is done in order to save resources and make our program as quick as possible, because then we
‫don't need to get the value and move the value around all the time.
‫But we can just say, okay, the value is at that location, so you can just get it from there instead
‫of then for example, copying that data around when we use it.
‫All right.
‫So when we copy this reference type of a data type, it will just copy the memory address of the data.
‫So we will then have two variables pointing at that same data.
‫So let's look at the pictorial representation here.
‫So we have this string called first named Dennis, and then we have the variable value address and then
‫the actual value.
‫So you can see here that.
‫This value here is just the address that you can see here, but the address of the variable name is
‫over here.
‫So the variable value address and the actual value are stored in two different locations in your RAM.
‫So overall, the variable value address is super short.
‫It's really just a very basic number, right.
‫But on the other hand, the actual value can be quite complex.
‫So it could be an object which is quite complex and has a lot of information stored in it.
‫And that's why it's better for our programs to use this approach or for C sharp in general.
‫So let's say you go to a rental, so you want to get a car.
‫Right.
‫So you go to a car rental and then you are at the counter and you want to pick up the car that you have
‫ordered.
‫Well, the counter doesn't have your car, but it has the location of your car.
‫So it will give you the keys and it will tell you, go this direction and you will find the car in row
‫so-and-so, column, so-and-so or floor, whatever.
‫So then you can just go there and you can pick up your car.
‫So instead of storing the car inside of the counter, which wouldn't work because the car would be too
‫big, they have all of these references where the cars are so that you can easily pick them up, even
‫though this example lacks a little bit of the rest of the complexity that we have here.
‫But maybe this helps you to understand the reference type a little better.
‫All right.
‫So that's it for now.
‫Let's get back to Cody.
‫See you in the next video.