Okay.
So in this video we're going to look at the base keyword.
And it is a solution to a problem because let's look at a problem that we have right now.
Let's say I am creating a dog and I want to access the Makesound method of the animal, which is this
method here, which makes an generic sound.
But in this case it's something very generic, right?
It's super simple.
It's just a Console.WriteLine statement.
However, most of the time it won't be something as simple and as useless so to speak, as this makes
sound.
Console.WriteLine.
Statement.
It could be something where there is a lot of initialization happening.
So this makes sound method could initialize a bunch of properties and set up the values.
And then we want to make sure that whatever deriving class we have and whatever our deriving class does
with that method is not going to cause any trouble because some properties aren't set or something is
missing.
So in that case, what we can do is we can use the base keyword inside of our dog class.
Inside of the Makesound method, what we can do is we can use this base keyword and say base dot makesound.
What this will do is it will call the animal's Makesound method when the Makesound method is called
on top of whatever our own implementation is.
So now we're not just overriding the old Makesound method or overwriting it, editing it, but we're
also accessing its execution, or we're calling it however it is.
Plus we're adding our own stuff to it.
So before we just said we only want to do our thing, now we're saying we want to do the base class
thing, and we want to do our specific implementation for that dog itself.
So if we run this now, we will see that we will get this animal makes a generic sound which is by our
dog, and then the dog makes the barking sound.
So as I said, this is a super generic example here, but you have to consider that sometimes you don't
have access to the base class.
You cannot change the base class.
Okay?
For example, somebody else in your team is responsible for the base class and is working on it, but
you don't have access to the base class code.
You cannot change it.
You can see what it does and you can use its methods.
You can override its methods by creating deriving classes, but you cannot change the base class itself.
Or when you're using third party code, such as frameworks that you didn't write yourself.
But they are defined in a way as they are defined.
And now what you need to do is you need to find your own way around it, but you cannot change whatever
there was before.
So in our example that we have here, it's all about animals and cats and dogs.
But consider real world examples where there is this class which has a bunch of properties, which has
a bunch of methods.
And now what I want to do is I want to use the base functionality of that class, but I want to extend
it to my own liking, to my own requirements.
And sometimes I still need to make sure that I execute the method as it was intended by the base class
before.
I can then go ahead and use the method well.
Modify what the method does on top of that by using, first of all, this base Makesound to run whatever
the base method of Makesound does, or the base class's method Makesound does, and then I add my own
code on top.
Okay, so in our example we are the user, right?
Or we're like the godlike developer who has access to everything.
But in the real world it's different.
You don't have access to everything, you cannot override all the code.
And if you were then to change, for example, what the base class does, some other developers who
are working with the base class wouldn't be aware of that.
And suddenly you're breaking the entire code base just because you said, you know what, I'm going
to change the base class as I want it.
And instead what you should have done is you should have changed your own method that you need to be
modified, or your own properties that you need to be modified.
Okay.
So that's just a little bit of side information that is very important.
However okay.
Okay.
So that's it for this video.
See you in the next one.