1 00:00:00,330 --> 00:00:01,290 ‫Welcome back. 2 00:00:01,320 --> 00:00:05,280 ‫In this video, we are going to look at the difference between value type and reference type. 3 00:00:05,280 --> 00:00:12,630 ‫So the data types can be categorized into two categories based on how they occupy memory location. 4 00:00:13,020 --> 00:00:16,650 ‫And we will look into those types in detail. 5 00:00:16,650 --> 00:00:19,080 ‫So first of all, we look at the value type. 6 00:00:19,080 --> 00:00:26,520 ‫So as the name suggests, it is a data type which when declared and assigned a value, holds that value 7 00:00:26,520 --> 00:00:29,310 ‫directly on its own memory space. 8 00:00:29,310 --> 00:00:29,670 ‫All right. 9 00:00:29,670 --> 00:00:37,110 ‫So the value type uses a heap to store the value example of value types are data types such as int float 10 00:00:37,110 --> 00:00:40,230 ‫long double char bool decimal double. 11 00:00:40,230 --> 00:00:45,780 ‫So basically the simple data types right now let's look at the pictorial representation of a value type. 12 00:00:45,780 --> 00:00:51,300 ‫So let's say we create a new variable called Z and it has the value of 234. 13 00:00:51,300 --> 00:00:58,860 ‫So now this value 234 will be stored in RAM at the position of this Z variable. 14 00:00:59,370 --> 00:00:59,820 ‫All right. 15 00:00:59,820 --> 00:01:05,640 ‫And then if we look at the reference type in more detail, we can see that it is a variable type which 16 00:01:05,670 --> 00:01:12,660 ‫instead of storing the value in memory directly, stores only the memory location of actual data. 17 00:01:13,590 --> 00:01:18,870 ‫The variable here stores the memory reference of the data and not the data directly. 18 00:01:18,900 --> 00:01:20,160 ‫This is the huge difference. 19 00:01:20,160 --> 00:01:20,610 ‫Right? 20 00:01:20,610 --> 00:01:29,010 ‫So reference data types are string class array, basically data types that are more complex and need 21 00:01:29,010 --> 00:01:30,870 ‫a lot more space, so to speak. 22 00:01:31,080 --> 00:01:37,140 ‫This is done in order to save resources and make our program as quick as possible, because then we 23 00:01:37,140 --> 00:01:40,740 ‫don't need to get the value and move the value around all the time. 24 00:01:40,740 --> 00:01:47,100 ‫But we can just say, okay, the value is at that location, so you can just get it from there instead 25 00:01:47,100 --> 00:01:51,720 ‫of then for example, copying that data around when we use it. 26 00:01:52,290 --> 00:01:52,560 ‫All right. 27 00:01:52,560 --> 00:01:57,870 ‫So when we copy this reference type of a data type, it will just copy the memory address of the data. 28 00:01:57,870 --> 00:02:02,940 ‫So we will then have two variables pointing at that same data. 29 00:02:02,940 --> 00:02:05,700 ‫So let's look at the pictorial representation here. 30 00:02:05,700 --> 00:02:12,960 ‫So we have this string called first named Dennis, and then we have the variable value address and then 31 00:02:12,960 --> 00:02:14,820 ‫the actual value. 32 00:02:14,820 --> 00:02:17,130 ‫So you can see here that. 33 00:02:18,210 --> 00:02:27,060 ‫This value here is just the address that you can see here, but the address of the variable name is 34 00:02:27,060 --> 00:02:27,810 ‫over here. 35 00:02:27,810 --> 00:02:35,430 ‫So the variable value address and the actual value are stored in two different locations in your RAM. 36 00:02:35,610 --> 00:02:40,680 ‫So overall, the variable value address is super short. 37 00:02:40,710 --> 00:02:43,830 ‫It's really just a very basic number, right. 38 00:02:43,830 --> 00:02:48,270 ‫But on the other hand, the actual value can be quite complex. 39 00:02:48,270 --> 00:02:52,800 ‫So it could be an object which is quite complex and has a lot of information stored in it. 40 00:02:52,800 --> 00:02:58,380 ‫And that's why it's better for our programs to use this approach or for C sharp in general. 41 00:02:59,800 --> 00:03:04,690 ‫So let's say you go to a rental, so you want to get a car. 42 00:03:04,720 --> 00:03:05,050 ‫Right. 43 00:03:05,050 --> 00:03:10,690 ‫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 44 00:03:10,690 --> 00:03:11,240 ‫ordered. 45 00:03:11,260 --> 00:03:16,270 ‫Well, the counter doesn't have your car, but it has the location of your car. 46 00:03:16,270 --> 00:03:23,260 ‫So it will give you the keys and it will tell you, go this direction and you will find the car in row 47 00:03:23,260 --> 00:03:26,500 ‫so-and-so, column, so-and-so or floor, whatever. 48 00:03:26,500 --> 00:03:30,160 ‫So then you can just go there and you can pick up your car. 49 00:03:30,160 --> 00:03:36,310 ‫So instead of storing the car inside of the counter, which wouldn't work because the car would be too 50 00:03:36,310 --> 00:03:43,030 ‫big, they have all of these references where the cars are so that you can easily pick them up, even 51 00:03:43,030 --> 00:03:47,860 ‫though this example lacks a little bit of the rest of the complexity that we have here. 52 00:03:47,860 --> 00:03:53,260 ‫But maybe this helps you to understand the reference type a little better. 53 00:03:53,920 --> 00:03:54,460 ‫All right. 54 00:03:54,460 --> 00:03:55,600 ‫So that's it for now. 55 00:03:55,600 --> 00:03:56,980 ‫Let's get back to Cody. 56 00:03:57,010 --> 00:03:58,210 ‫See you in the next video.