Welcome back.
In this video, I want to show you how you can, in a meaningful way, modify what happens when the
user tries to get your property.
So when we want to get the brand from outside, for example, instead of just setting it, which we
modified last time, we're going to look at how we can get it in a meaningful way.
Therefore, I, however, need a little bit more code.
So I'm going to add something so that it becomes more appealing to understand it and easier to understand
it as well.
So I'm going to create another private member variable which will be a bool which will just hold information
about is luxury.
So is it a luxury car or not.
And what I want is I want to add that to the constructor.
So here I'm going to say bool is luxury and I'm going to not set that to defaults.
I'm going to say okay now here we need to set is luxury.
So for now I'm going to say is luxury is luxury okay.
So our member variable is going to be oh well actually it already added it down here okay.
So our member variable is luxury is going to be whatever the user entered as whether it's a luxury car
or it's a not a luxury car.
So maybe just a luxury version of the A3 for example, or the sports version of the A3 or whatever.
So now we obviously need to modify this here as well.
So I could say, okay, this one is going to be a luxury car.
So the A3 or actually let's do it the other way around because the A3 can be a non luxury version and
the i7 could very well be the luxury version of the i7.
Okay.
So what I'm doing here now is I'm saying we're creating an Audi that is an A3 Audi with not being luxury,
and then a BMW that is actually a luxury car.
Okay.
So here you see we are directly accessing the private member variable, which we shouldn't do this way.
Right.
So what we need is we need a property that surrounds it.
How can we create a property that surrounds.
This is luxury.
Well we can use the right click.
Quick actions encapsulate this luxury and property like so and then it will create this.
Bool is luxury property for us down here.
You can see it.
And it also overrode this piece of code.
So the IDE that a lot of work for us, we didn't even need to manually do any of that.
So that's pretty cool.
So now we just added a little more details to our car.
Now we have more information about the car that we can store.
So now what I want to do is I want to modify what should happen when you want to get the brand, because
I'm going to also say that it is the luxury version.
So how can I do that?
Well, I can use the get keyword and then in curly brackets I can say if let's say is luxury is true,
then return something like Brand Plus Luxury Edition and then else just return the brand.
Okay, so what I'm just saying here is that whenever you access the brand, I will first of all check
something else and then I will give you a modified result.
So I'm not just going to give you the brand all the time.
But sometimes in case that your car is a luxury car, I'm going to give you the brand plus the Luxury
Edition.
So I'm going to give you even more information.
So that means that if we now try to access the brand here and let's access the brand for our not the
Audi, well, actually, let's access it for both.
Let's access it for Audi and for our BMW.
Because.
Because they are different, right?
The Audi is not a luxury car and the BMW is a luxury car, or specifically this version that we are
using here.
Okay.
There are luxury Audi's as well.
And there you see it, it says brand is Audi and brand is BMW Minus Luxury Edition.
So what we did there is we modified what you get when you use the Get method.
And this is the get that this is internally like a Get method.
It's not written like such, but it is pretty much like you were trying to get the brand.
Like technically this is a getter method, but that's too much for this point of the video.
But now you saw how you can use encapsulation to basically use your member variable, hide it from outside
classes, but also make sure that the only way to access it is to go through this gatekeeper called
the property that encapsulates it.
So this is our property that makes sure that it's own defined rules are considered when the brand is
gotten, as well as when the brand is.
Set so when it is overwritten.
So that is the idea behind properties and why we're not just saying public brand here for example public
string brand brand or so.
So we should not do this okay.
We should always make them private, but we can use this really cool way around it to actually access
the variables.
And yeah, that's pretty much enough for now.
I would say when it comes to properties, one thing that I didn't explain, and I'm sorry about that,
but I really don't want to go that deep into the rabbit hole of advanced C-sharp programming.
Yet.
I didn't explain lambda expressions.
We're going to do that however, later on in the course.
For now, just accept that this is a lambda expression, and this here is just a simplified version
of this.
Okay.
So these two, this line here as well as this code here, they result in the same thing.
They do the same thing, only that this is obviously a lot less space that is required.
It's just one line of code that does all the magic.
And here we needed to write it out in multiple lines to make it look clean.
So you see ten lines for one property, one default property in order for it to look good.
So you see here when you try to get we just return the underscore model.
When you try to set we just set the underscore model to whatever value you give me.
Okay.
So this here let me comment it out all the way down here.
This is by the way a different way of commenting.
This is a multi-line comment that we're doing here with slash asterisk and asterisk slash.
In order to comment out all of these lines here.
And you can see that it is the same thing as here, only that here we are using lambda expressions.
This here is a lambda expression which really simplifies this code here okay.
So that's it for this video.
I hope you enjoyed it.
And now you understand properties.
In the next few videos we're going to look more into object oriented programming concepts.
So see you there.