‫Welcome back.
‫In this video, we're going to talk about structs.
‫And structs are very similar to classes, but classes are reference types and structs are value types,
‫which means if you create an object of a class, it can contain nothing.
‫So it can be empty, but a struct has to have a value.
‫Let's create a struct called game, so you use the struct keyword and then you give it a name.
‫So it's very similar to a class from what you can see.
‫So you have this name here and then you can go ahead and create variables in here.
‫So I'm going to create one which is called name, then I'm going to create one that is called developer
‫and one that is a rating.
‫So how is this game rated and then public string release date?
‫So when was it released?
‫So that's a simple struct.
‫Now we can go ahead and create an object of that struct so we can go ahead and say, okay, game, game
‫one like that.
‫So now we have this game.
‫But in order to use that game, we need to go ahead and specify it.
‫So we need the specification of the game.
‫One, for example, we need to set the name and let's say it was Pokémon Go, then game one needs to
‫have a developer.
‫So we're going to say something like Niantic and then Game one needs to have a rating.
‫Let's say it was 3.5 out of five stars and finally the release date.
‫So you can simply set the values like that.
‫So it's very similar to what you would do with a class, right?
‫So it was released 2016 and that's our game struct that we have here.
‫Now we can go ahead and write game information onto the console.
‫So let's say we want to write the name of the game.
‫So CW Game one.
‫And his name is and then curly brackets zero and game dot name or actually game one name.
‫So as you can see, you can simply go ahead and call it variables so we can do the same thing, not
‫not just with the name.
‫We can do that with everything else.
‫So I'm just going to copy that.
‫Game one was developed by and now it's not the game name, but it's the developer.
‫And the same here.
‫Rating is game one's.
‫Rating is and here it's rating.
‫And finally, game one was released in game one re release date.
‫Now let's run it.
‫Console the red key and let's see what's happening.
‫And we are a game.
‫One's name is Pokemon Go.
‫Game one was developed by Niantic.
‫Rating is 3.5 and it was released on the 1st of July 2016.
‫All right.
‫So that we are.
‫It's very basic and very similar to a class.
‫Right.
‫So how does it differ?
‫Well, there is one more thing where it doesn't differ.
‫So I'm going to create public void display, which should display information.
‫And what I'm going to do is I'm simply going to execute all of that in there.
‫So it's going to give me the name.
‫It's going to give me the developer.
‫It's going to give me the rating and the release date.
‫All right.
‫Now, instead, I can simply call the display method and I can do so by calling game one and saying,
‫please display all the information.
‫So now let's run it again and we will see we get all the information.
‫All right.
‫So you can create variables.
‫You can create methods.
‫But how does it differ?
‫Well, what you cannot do is you cannot create a constructor.
‫Structs cannot contain explicit parameter less constructors.
‫So you cannot create default constructors in here.
‫Then it can implement one or more interfaces.
‫So you could go ahead and implement interfaces here i cleanable i comparable those kind of things.
‫Then they do not support inheritance.
‫So you cannot use inheritance on structs.
‫You cannot have a default constructor.
‫As I said then struct members cannot be specified as abstract, virtual or protected, so they can just
‫be something like public.
‫So you can have a public struct or you can have a private struct.
‫Structs can however have defined constructors.
‫So you can have something like public game and then here you have the string name.
‫You can have that and that will be name or this name is equal name and this dot developer is equal developer.
‫The star rating is equal rating.
‫And this DOT release date is equal release date.
‫Now, you, of course, need to have them in here.
‫String developer, then double rating and string release date.
‫But then if you, for example, want to go ahead and display the display method here, then you run
‫into an error because use of unassigned local variable game one.
‫So game one was not assigned.
‫So even if I tried, let's say after having all the information by far by here, you see there's still
‫an error.
‫So you need to have all the different information about this game one to be assigned.
‫Otherwise, this play doesn't work.
‫So even here, after we have the rating, we still get the same error.
‫Only after we have assigned all the values, then we can work.
‫So it works by value, not by reference, like a class would do.
‫Right.
‫So if you want to have a short summary, StackOverflow is your friend.
‫So a short summary of each class's support inheritance.
‫Our reference prototypes can be null and have memory over heap per new instance.
‫Structs only cannot support inheritance.
‫Our value types are passed by value similar to integers then cannot have a null reference as you have
‫just seen and do not have a memory over heap per new instance.
‫And both have a compound or are compound data types typically used to contain a few variables that have
‫some logical relationship, can contain methods and events and can support interfaces.
‫Even though Simon Stephens argues that classes do not always go on heap and struck, do not always go
‫on stack.
‫All right.
‫And this StackOverflow entry in general has quite some valuable information about structs.
‫So the difference is between structs and classes.
‫But you have seen most of them in here.
‫But as usual, I always recommend to check out the documentation as well to get a better understanding.