In this video we're going to look at properties.
And properties are an important aspect of encapsulation, which is an important part of object oriented
programming.
And what it does is it shields variables from outside classes.
So we are already shielding variables from outside classes.
But that's not enough.
We also want to have control over how this variable model, for example, can be accessed from the outside
and how it can be changed, how it can be modified.
Because right now we cannot neither access it nor modify it from the outside.
We can try it.
We can say here, for example, Audi dot underscore and then call it model here and then try to assign
something to it.
But this will cause an error if you hover over it.
It says car dot model is inaccessible due to its protection level.
So what is the protection level?
Well currently the protection level is private.
So this keyword here is setting the protection level.
So private just means that this variable model can only be modified inside of the same class.
Now if I change that to public for example, then suddenly this error disappears because now I can modify
it because the protection level is not private anymore.
It's public now.
So now outside classes like our Program.cs class have access to it.
Now the problem here, why is that?
This is very bad programming.
This is not something we should do.
So the idea behind encapsulation and in general object oriented programming is that you have cleaner
ways of modifying it.
And why is that?
Because you need to be able to define when and how the model variable can be accessed.
Now we can create a surrounding property for it.
So how would we do that.
We can use something called a property okay.
So we can create our own property like this.
We can use the prop keyword then press tab.
Then define what data type we want it to be.
So I'm going to use string.
Press tab again and again.
Then give it a name.
And you see a property is with the capital letter starts with the capital letter.
So I'm going to use model here.
So this here is a property okay.
So we have get and set here which just means that we can modify how this property can be gotten and
how we can modify how it can be set.
So how it can be overwritten.
Now there are multiple ways how I can make this be surrounded or protected by a property.
Now you can right click and go over quick actions and refactorings.
You can hover over down here, encapsulate and use property like so.
So you see what it did there?
Uh, probably you didn't see it.
It's a little difficult to see.
Like you go over here and then you go down to encapsulate field and use property like this, this field
here where it says end use property.
If you click on that, then it will create this code here.
Now this code uses something called lambda expressions.
So this here is a lambda expression.
But basically what it says is whenever you want to get the model.
And let me get rid of this one here, whenever you want to get the model here, you can access it.
I will give you the model whenever you want to set the model, whatever value you give me, I'm going
to set that as the model.
Okay.
And now you see also that my code automatically protected my model.
My private variable model here the member variable.
It protected it automatically down here as well.
So it overrode my underscore model.
This is because it's part of encapsulation of making sure that your variable is encapsulated and you
have full control over how it is set and gotten.
So here you can define that okay.
We're going to do that step by step in a bit.
But I just wanted to show you how you can very quickly do that using the refactoring feature.
Now we can do the same thing with our brand.
So let's right click here.
Go to Quick Action and Refactorings and say encapsulate field brand and use property.
So now it used this code here created this code here you can see it's the same as we had with the model
where it just says now there is this property called brand.
It's public.
It's a string.
When you want to get it I'm going to give you my underscore brand when you want to set it, whatever
value you give me, I'm going to store that inside of my member variable brand.
And then you can see that down here before it said under.
Your brand should be overwritten, but now just modified it and said, okay, you don't get access to
this private member variable.
The only person slash the only code that has access to it is the property itself.
So only the property brand can now modify brand.
Nowhere else should we modify underscore brand in our code.
If you want to modify underscore brand, we need to go through this protector, this gateway of brand.
Because brand defines who can get in and who cannot get in.
So for example, what we can do is we can say that only if you give me a value that is not null, I'm
going to accept it.
Or only if you get me a value that is an acceptable string that let's say has a length of ten characters
maximum or so.
Only then will I accept your entry and that is something we can define ourself here now.
And that's what we're going to look at in the next video.
So see you there.