Now that we have seen how read only properties work, what about write only properties?
Well, a really good example for a write only property would be the password.
Okay, so if we have a customer and that customer needs to have a password to log into their account
on our website, we need to store the password somehow.
Right?
And if we want to make sure that the password cannot be exposed by anything, by any of our methods
or whatever, that's where we would use the write only properties.
So let's go ahead and create a new private variable here private string underscore password.
And this one will be our backing field for the write only property.
Now the write only property I'm going to use prop string.
And I'm going to call it password.
Now this one should not have a getter but it should have a setter that we define ourselves.
So what do we want to put in here.
We want to set the password to be whatever we set to the password.
So this here now becomes a write only property.
Because the password can only be set, it cannot be read.
And that's something you see on websites, right?
You go to a website and let's say you forgot your password.
It's not going to send you your password because it can't read the password.
That would be really risky, so to speak.
But what it does is it says, okay, please set a new password.
Now, obviously it somehow needs to compare the passwords later on and so forth, but that's something
we can expose later on when we're looking at user logins in depth.
But here it's really just about the general concept of write only properties.
So let's say we do have now this customer one, two, three and so forth.
What we can do is we can say customer three dot password and then we can assign a password to that user.
So let's say something like whatever a random string here.
So that would now be our password.
And let's add some special characters in here so that it becomes even more safe.
Okay.
So now our user customer three has a password.
And if I want to access it and write it out I cannot do it.
So customer three dot password.
This will cause an error because the property or index or customer password cannot be used in this context
because it lacks the get accessor.
So what is the get accessor?
Well, the get accessor is this thing here.
This get the name has a get accessor.
Our ID has a get accessor, but our string password only has a set.
So we don't have the get accessor here.
We cannot access the password right?
We can only set it.
So now we've set the password, but only for one customer.
Customer three not for the others.
And that's what we could do at other points of our program.
And now you also see why it's so important that we don't set the password backing field here.
Because this one here is exposable.
So we can actually get its value.
So in code we could go ahead.
And when we get our details so get details we could say ID is so and so and then and the password is.
And then we can use our underscore password here.
So suddenly we do have access to this password.
And this is a really good example why properties are so important.
Because now we can actually expose the password.
So let's write or run this.
And let's actually get the details of customer three here.
So like so.
And now we see the password of our user three or our customer three.
So this is why it's so important not to use the backing fields outside of defining the property itself.
So you see here here we are setting the password.
And now if we want to access the password and we want to use the password, we should always just use
the property.
And you see we cannot access the property even inside this class.
That's because the private the property does not have the get accessor.
You can see that here again.
So this is really really crucial.
And that's also saving ourselves from using and exposing information that should not be exposed.
Okay.
So let's get rid of this part here again.
But.
Now you know how the whole thing works.
Now, obviously, there are more advanced techniques in order to even allow us not to access the password
and like, use hashes and all of that stuff, but that's something for later.
All right.
So see you in the next video.