Welcome back.
In this video, I really want to make sure that you understand the difference between a reference type
and a value type, okay.
And as I said, structs are value types.
They are not reference types.
Classes are reference types.
Now what is the difference?
Let's look at it.
So what we have here is we have our struct point with an x and y value, and constructor with an x and
a y value, and the display method that just displays those points.
And then I have this same structure for my class, because the difference is that classes, as I said,
can be inherited from structs can't.
And as I also said, value type and not value type.
So these are the biggest differences as I said.
And this class has the same things like also an X and a y value.
It also assigns these values and then it displays them Okay.
So up to that point they are the same.
Now how do they differ.
So what I have here is I have a point one at 1020 I display that point, I have a point two which is
point one.
So I just assign point one to it.
So p two now is a copy of p one.
Then I display p two and then I change p to x.
So the x value of p2 I changed that to 25.
So it's not going to be to be 20 anymore.
But it's going to be 25.
So this will change P2 but it will not change P1.
So let me actually comment this part out first so that we see what's going on.
So after that I also displayed the two piece okay.
So we see point one is ten 20.2 is 1020 After I change p to x to 25, you see point one is still 1020
but p two is 2520.
Okay, so all up to this point totally fine.
Nothing too scary, but we just see that we are copying P1 into P2.
So the value of it not the reference to the value itself.
So now let's uncomment this and let's run it again after I explained it.
So I say now come the class objects I create a PC1 object that is a point class object, which is pretty
much a point right.
And I set that to one two.
Then I create another object.
So I call the PC2 which is like point two right for our class and I assign PC1 to it.
So here PC2 is a reference to the same object as PC1.
So it just points to the same location in the stack in the memory of our computer or processing unit.
So it's the same location.
So it holds the same value while holding the same location.
But it's not independent, so to speak.
So now I display those two.
Then I do the same thing as I did earlier where I changed p to x the x value.
Now this time I change it to three.
So what this will do is it will change p1 x as well because p1 and p2 reference the same object, so
they are pointing at the same object in the memory.
So then I say after changing TX to x to three and then I displayed them again.
So now let's run this and you will see that here come the class objects.
And even though I only changed PC2 x to three now pc1 x is also three.
And that's what I was talking about when I was saying one is a value type and the other one is a reference
type.
Classes are reference types, so they are referring to an object in the memory structs.
However they just have a value.
They just say whatever.
You know, this is the value that I hold.
And if you want to have it, I just give you the same values.
You copy the same values over to yourself and that's it.
So that's why you cannot easily compare to structs to each other, because you can only compare if they
hold the same value.
You cannot compare if they are the same object for classes, however, you can do that.
You can compare if two objects are equal to each other.
There is this equal method that any object has.
Right?
Because objects or classes inherit from the object class.
So you could say pc1 dot equals PC2 and this will be a bool.
So bool is equal.
And now let's display is equal here as well.
Is it equal.
Plus is equal.
Now let's run it again.
And we should see that they are equal.
So it says is equal true.
So this just shows us that those two objects, while we gave them different names internally they are
the same.
So PC1 and PC2 of the class are the same because they are pointing to the same object in the memory.
Now I said it ten times.
I think you got the point.
So that's it for this video.
See you in the next one.