Welcome back.
In this video we're going to look at structs.
And we're going to potentially look at into it maybe a little more than just one video.
So structs are very similar to classes.
They have most of the same capabilities, but they differ in a few main aspects and also when they are
used.
So we're going to look at a struct later on that is called DateTime, a really powerful struct that
allows us to use time basically in our application and know which date it is and so forth, which is
very important to know as a developer.
But therefore, I thought it would be really wise to first look at structs and understand how they work.
So the difference, the biggest difference between the two is that structs are value types, meaning
they are stored on the stack and passed by value, whereas classes are reference types where they are
stored on the heap and passed by reference.
We're going to see what that means in an actual example later on, than it will make a lot more sense,
because what I just said is very abstract.
But when we have an actual example, it will make so much more sense.
Structs are ideal for small, lightweight objects that don't require inheritance.
That's the next big difference between the two.
Okay, so structs allow no inheritance.
Classes do.
So these are the two biggest differences.
We're going to see the other differences as well.
But let's go ahead and build a struct to get a feel for it.
So I'm going to create this public struct and I'm going to call this one point okay.
I'm making it public so that I can access it later on.
And in the struct I'm going to have two properties.
So one is going to be an int x and another one is going to be an int y.
Okay.
So I'm just going to create these two properties.
And then structs can also have constructors.
So Ctor.
And this point will require an int x and an int y.
And what I want to do is I want to set x to x and y to y.
And structs can also have methods okay.
So a struct can have a method public void.
And this one will be display.
And what it will do is it will just display the x and y point.
So CW and I'm going to say dollar sign and point is at.
And then the X position as well as the y position separated by a comma and inside of parentheses.
So now let's go ahead and use our struct.
Now it's the same way as we would use a class.
So we just go ahead and say point for example P1 new point.
And then you see I didn't add anything into the point constructor here.
But there is another overload okay.
When you hover over it you see there is another overload, which means that there is a default constructor
that exists in a struct.
So in our case here public point.
You see that's our own overload or our own custom constructor that we built where we take in an X and
a y value.
And that's also what I'm going to put in here.
Okay.
So point P one.
So we can just use p one dot display.
And that will allow us to display whatever is inside of that struct.
And we will see it just works like a normal method.
It writes the point onto the console.
Okay, now another difference is that structs can be declared without the new keyword.
So I can just say point P2 is not new point, but actually just exists.
Okay.
And then I can say p2 x is going to be ten and p2 y is going to be 20.
And then I can go ahead and say p two dot display.
If what I said is true, that we can use structs without initializing them with a new keyword, why
do I get an error here?
And that is because I'm using properties here.
If I were to use fields, what I just said would work ideally.
So let me just show you an example.
So I'm just going to copy this and put it down here.
Comment those out.
And instead of this being a property it's going to be a field.
Okay.
So now if we look at our code we see that we don't get an error.
So the only thing that is important is that all fields must be initialized at the point of when they
are used.
So in the display method we are using these fields, right.
And because we do so they need to be initialized at that point.
That's what our constructor usually takes care of right.
We just said okay, you know what.
Give us the two values and then we can use them.
So that's what we did in this case.
But in this case you see there is no new keyword.
We did not do any initialization.
And that works because as I said those two are fields.
Okay.
So let's run this and see if it still works.
And it actually does work.
So we have point one and point two.
Okay.
Now the next thing that I want to show you is that structs are value types.
So let's say we have a point three and it's called P3.
And now it will not be a new point but it will be P1.
So now let's say I set p3 x to 50.
Then I want to display P1 and display P2 or actually P3 in this case.
Right.
So there we are.
And we will see that this is point one and this is point two.
So we overwrote.
And the cool thing is we could just take the values from point one.
And it was 1020 4.3.
So this is point three and now it's 50.
Okay.
So the x value of it was overwritten.
So we could just assign the value of P1 into P3.
It's not like we actually put the P1 object so to speak into P3.
We only passed on the values from P1 to P3.
This will make more sense once we actually compare classes to that as well.
I'd say that's it for now.
Let's look into structs in the next video a little more.