1 00:00:00,450 --> 00:00:01,290 ‫Welcome back. 2 00:00:01,320 --> 00:00:03,450 ‫In this video, we're going to talk about structs. 3 00:00:03,450 --> 00:00:11,430 ‫And structs are very similar to classes, but classes are reference types and structs are value types, 4 00:00:11,430 --> 00:00:16,350 ‫which means if you create an object of a class, it can contain nothing. 5 00:00:16,350 --> 00:00:19,800 ‫So it can be empty, but a struct has to have a value. 6 00:00:19,950 --> 00:00:26,190 ‫Let's create a struct called game, so you use the struct keyword and then you give it a name. 7 00:00:26,190 --> 00:00:29,280 ‫So it's very similar to a class from what you can see. 8 00:00:29,280 --> 00:00:33,690 ‫So you have this name here and then you can go ahead and create variables in here. 9 00:00:33,690 --> 00:00:39,510 ‫So I'm going to create one which is called name, then I'm going to create one that is called developer 10 00:00:40,230 --> 00:00:44,760 ‫and one that is a rating. 11 00:00:44,850 --> 00:00:50,970 ‫So how is this game rated and then public string release date? 12 00:00:50,970 --> 00:00:52,410 ‫So when was it released? 13 00:00:52,680 --> 00:00:54,360 ‫So that's a simple struct. 14 00:00:54,360 --> 00:01:02,460 ‫Now we can go ahead and create an object of that struct so we can go ahead and say, okay, game, game 15 00:01:02,460 --> 00:01:04,560 ‫one like that. 16 00:01:04,560 --> 00:01:06,030 ‫So now we have this game. 17 00:01:06,030 --> 00:01:11,880 ‫But in order to use that game, we need to go ahead and specify it. 18 00:01:11,880 --> 00:01:14,190 ‫So we need the specification of the game. 19 00:01:14,190 --> 00:01:22,530 ‫One, for example, we need to set the name and let's say it was Pokémon Go, then game one needs to 20 00:01:22,530 --> 00:01:23,640 ‫have a developer. 21 00:01:24,750 --> 00:01:32,550 ‫So we're going to say something like Niantic and then Game one needs to have a rating. 22 00:01:33,250 --> 00:01:40,380 ‫Let's say it was 3.5 out of five stars and finally the release date. 23 00:01:40,740 --> 00:01:43,770 ‫So you can simply set the values like that. 24 00:01:43,770 --> 00:01:48,120 ‫So it's very similar to what you would do with a class, right? 25 00:01:48,540 --> 00:01:55,770 ‫So it was released 2016 and that's our game struct that we have here. 26 00:01:55,770 --> 00:02:00,540 ‫Now we can go ahead and write game information onto the console. 27 00:02:00,540 --> 00:02:03,630 ‫So let's say we want to write the name of the game. 28 00:02:03,630 --> 00:02:07,590 ‫So CW Game one. 29 00:02:09,980 --> 00:02:22,610 ‫And his name is and then curly brackets zero and game dot name or actually game one name. 30 00:02:23,570 --> 00:02:29,480 ‫So as you can see, you can simply go ahead and call it variables so we can do the same thing, not 31 00:02:29,780 --> 00:02:30,860 ‫not just with the name. 32 00:02:30,860 --> 00:02:33,200 ‫We can do that with everything else. 33 00:02:33,210 --> 00:02:34,910 ‫So I'm just going to copy that. 34 00:02:35,000 --> 00:02:43,490 ‫Game one was developed by and now it's not the game name, but it's the developer. 35 00:02:44,120 --> 00:02:45,800 ‫And the same here. 36 00:02:47,420 --> 00:02:50,540 ‫Rating is game one's. 37 00:02:51,680 --> 00:02:55,580 ‫Rating is and here it's rating. 38 00:02:56,210 --> 00:03:05,960 ‫And finally, game one was released in game one re release date. 39 00:03:08,320 --> 00:03:09,430 ‫Now let's run it. 40 00:03:10,840 --> 00:03:17,110 ‫Console the red key and let's see what's happening. 41 00:03:18,800 --> 00:03:19,880 ‫And we are a game. 42 00:03:19,880 --> 00:03:21,440 ‫One's name is Pokemon Go. 43 00:03:21,470 --> 00:03:23,780 ‫Game one was developed by Niantic. 44 00:03:23,960 --> 00:03:29,120 ‫Rating is 3.5 and it was released on the 1st of July 2016. 45 00:03:29,150 --> 00:03:29,750 ‫All right. 46 00:03:29,750 --> 00:03:31,310 ‫So that we are. 47 00:03:31,340 --> 00:03:35,030 ‫It's very basic and very similar to a class. 48 00:03:35,030 --> 00:03:35,600 ‫Right. 49 00:03:36,590 --> 00:03:37,970 ‫So how does it differ? 50 00:03:38,000 --> 00:03:40,430 ‫Well, there is one more thing where it doesn't differ. 51 00:03:40,910 --> 00:03:47,270 ‫So I'm going to create public void display, which should display information. 52 00:03:47,270 --> 00:03:52,070 ‫And what I'm going to do is I'm simply going to execute all of that in there. 53 00:03:52,070 --> 00:03:54,770 ‫So it's going to give me the name. 54 00:03:54,770 --> 00:03:56,750 ‫It's going to give me the developer. 55 00:03:56,900 --> 00:04:00,800 ‫It's going to give me the rating and the release date. 56 00:04:02,000 --> 00:04:02,330 ‫All right. 57 00:04:02,360 --> 00:04:09,110 ‫Now, instead, I can simply call the display method and I can do so by calling game one and saying, 58 00:04:09,110 --> 00:04:11,570 ‫please display all the information. 59 00:04:11,840 --> 00:04:15,860 ‫So now let's run it again and we will see we get all the information. 60 00:04:16,190 --> 00:04:16,580 ‫All right. 61 00:04:16,580 --> 00:04:18,740 ‫So you can create variables. 62 00:04:18,740 --> 00:04:19,670 ‫You can create methods. 63 00:04:19,670 --> 00:04:20,520 ‫But how does it differ? 64 00:04:20,540 --> 00:04:27,980 ‫Well, what you cannot do is you cannot create a constructor. 65 00:04:28,520 --> 00:04:32,480 ‫Structs cannot contain explicit parameter less constructors. 66 00:04:32,690 --> 00:04:35,300 ‫So you cannot create default constructors in here. 67 00:04:35,840 --> 00:04:38,840 ‫Then it can implement one or more interfaces. 68 00:04:38,840 --> 00:04:44,930 ‫So you could go ahead and implement interfaces here i cleanable i comparable those kind of things. 69 00:04:46,070 --> 00:04:48,950 ‫Then they do not support inheritance. 70 00:04:48,950 --> 00:04:51,770 ‫So you cannot use inheritance on structs. 71 00:04:51,980 --> 00:04:53,750 ‫You cannot have a default constructor. 72 00:04:53,750 --> 00:05:01,460 ‫As I said then struct members cannot be specified as abstract, virtual or protected, so they can just 73 00:05:01,460 --> 00:05:03,080 ‫be something like public. 74 00:05:03,080 --> 00:05:06,110 ‫So you can have a public struct or you can have a private struct. 75 00:05:08,590 --> 00:05:11,320 ‫Structs can however have defined constructors. 76 00:05:11,320 --> 00:05:17,500 ‫So you can have something like public game and then here you have the string name. 77 00:05:18,280 --> 00:05:30,790 ‫You can have that and that will be name or this name is equal name and this dot developer is equal developer. 78 00:05:32,260 --> 00:05:35,200 ‫The star rating is equal rating. 79 00:05:35,200 --> 00:05:39,640 ‫And this DOT release date is equal release date. 80 00:05:39,670 --> 00:05:41,710 ‫Now, you, of course, need to have them in here. 81 00:05:41,710 --> 00:05:50,050 ‫String developer, then double rating and string release date. 82 00:05:50,980 --> 00:06:00,910 ‫But then if you, for example, want to go ahead and display the display method here, then you run 83 00:06:00,910 --> 00:06:05,690 ‫into an error because use of unassigned local variable game one. 84 00:06:05,710 --> 00:06:08,320 ‫So game one was not assigned. 85 00:06:08,320 --> 00:06:15,490 ‫So even if I tried, let's say after having all the information by far by here, you see there's still 86 00:06:15,490 --> 00:06:16,060 ‫an error. 87 00:06:16,060 --> 00:06:21,520 ‫So you need to have all the different information about this game one to be assigned. 88 00:06:21,550 --> 00:06:23,380 ‫Otherwise, this play doesn't work. 89 00:06:23,380 --> 00:06:27,490 ‫So even here, after we have the rating, we still get the same error. 90 00:06:27,520 --> 00:06:30,940 ‫Only after we have assigned all the values, then we can work. 91 00:06:30,940 --> 00:06:36,390 ‫So it works by value, not by reference, like a class would do. 92 00:06:36,400 --> 00:06:37,000 ‫Right. 93 00:06:38,050 --> 00:06:42,760 ‫So if you want to have a short summary, StackOverflow is your friend. 94 00:06:42,760 --> 00:06:47,410 ‫So a short summary of each class's support inheritance. 95 00:06:47,410 --> 00:06:52,840 ‫Our reference prototypes can be null and have memory over heap per new instance. 96 00:06:52,840 --> 00:06:55,900 ‫Structs only cannot support inheritance. 97 00:06:55,900 --> 00:07:02,950 ‫Our value types are passed by value similar to integers then cannot have a null reference as you have 98 00:07:02,950 --> 00:07:07,390 ‫just seen and do not have a memory over heap per new instance. 99 00:07:07,390 --> 00:07:13,360 ‫And both have a compound or are compound data types typically used to contain a few variables that have 100 00:07:13,360 --> 00:07:19,180 ‫some logical relationship, can contain methods and events and can support interfaces. 101 00:07:20,980 --> 00:07:27,640 ‫Even though Simon Stephens argues that classes do not always go on heap and struck, do not always go 102 00:07:27,640 --> 00:07:28,360 ‫on stack. 103 00:07:28,690 --> 00:07:29,020 ‫All right. 104 00:07:29,020 --> 00:07:34,780 ‫And this StackOverflow entry in general has quite some valuable information about structs. 105 00:07:34,780 --> 00:07:37,630 ‫So the difference is between structs and classes. 106 00:07:37,630 --> 00:07:39,880 ‫But you have seen most of them in here. 107 00:07:39,880 --> 00:07:45,880 ‫But as usual, I always recommend to check out the documentation as well to get a better understanding.