Welcome back.
In this video, I want to show you why every single class in C sharp is inheriting from the object class.
So what does that even mean?
Well, let's use the example that we had from earlier where we had the manager, Carl and so forth.
Now it doesn't really matter too much about which object you are using this on, but Carl is just an
object that I already have.
Now you see that there are a few methods that I never created myself, like two string or get type or
get hash code or equals.
So all of these are methods that are inside of the object class.
So you see here bool object equals determines whether the specified object is equal to the current object.
So that would be to compare two objects to each other and it will return true if that is the case or
false if that isn't the case.
So sometimes you just want to compare two objects with each other and see if they are the same.
But most of the times they won't be, except it takes actually the same object.
So it might be that you have two lists and you have the same object in both lists, and you want to
compare the two lists to each other and see if there are any duplicates.
So that would be something you could do.
Then there is this two string.
This is the simplest one and I believe we have used it two degree already.
So what this will do is it will return a string that represents the current object.
So let's actually just display two string okay.
And let me actually get rid of this display manager info.
I really just want to know about the string Carl.
So let's run this and we see well okay.
Person constructor has been called employee constructor has been called.
That's fine.
And then we see inheritance app dot manager.
So that's all the info that we get from this two string method.
Now what is happening with two string or what is this object class.
Let's jump into the two string.
You can hold the control key.
And that will then send you over to this object class.
And you can scroll up here.
You can see it's in the namespace system.
It has a bunch of modifications here.
So it's serializable.
It's class interface and so forth a bunch of really advanced stuff.
But what matters for us is that you see that this is a class and it's called object.
Now it's a partial class.
We're going to see what partial classes are later on.
But for now you see that this is a class called object.
You can see that it has a constructor.
Then again it has a notation like the annotations here.
Let's not worry about those.
So then we see that there is this two string method which is the one that we just used.
And it returns the type two string.
Now going into get type would go a little too far at this point of the video or of the course, but
I think we're fine with just understanding that it gets the type and transfer and gives us the string
or converts it into a string.
And then we have this method called equals which allows us to compare two objects.
So it just says is the object that we're passing to this equals method.
Is it the same as this object that is calling the method itself?
Okay.
So that's what this equals does.
That's when you're doing it with one object.
But then if you compare two objects to each other then you can pass more.
You see here if you pass two then that's what this equals does.
So then there is this reference equals and there's the gethashcode.
So you see a few methods that all do different things.
You can obviously read through the summary and get a better understanding.
But this gethashcode serves as a default hash function.
Returns a hash code for the current object.
So Gethashcode is intended to serve as a hash function for this object based on the contents of the
object.
This hash function will return a suitable value with a relatively random distribution over the various
inputs.
So this just allows us to create a randomized hash code, so to speak, which then makes this object
unique in the sense okay, so every single object gets its own hash code.
And with the hash code you can compare them to each other and you can find out whether they are the
same object or not.
So pretty cool stuff.
And I don't want to go too far deeper into it, because you see, there are so many other keywords that
we haven't covered yet, and that would go quite far.
But I just wanted to show you that this is what's going on, and every single class in C sharp is inheriting
from the object class, which allows us to use methods such as two string onto a class which we have
created ourselves, or onto an object of which we have created the class without having implemented
this method.
Okay, so that's it.
For this video, and I would just say explore this a little bit if you want to dive deeper, but don't
go too far because it's a deep rabbit hole that you can get into.
And I believe we're not ready yet at this point of the course.
So see you in the next video.