Welcome back.
In this video we're going to look at keys as well as the read only keyword.
So what are keys.
Well the key is the unique identifier inside of a database.
So we're not working with databases yet.
But while we're dealing with customers, I thought it would be a good idea to also explain how IDs work,
because we learned that there is this static keyword that allows us to increment itself each time that
a new customer is created, which then we can use, for example, for such a key.
The idea behind the key is that it is allowing every single entry.
So in our case, every single customer to have something that is unique about them, because it could
very well be that we have John Doe as our customer entry, but we have ten John Doe's in our database
of millions of people or hundreds of John Doe's.
So what are we going to do then about them?
How do we know which John Doe is which?
Well, that's where the key comes in, where it's a unique identifier.
And usually it's just the ID, a number that you assign to the entry.
And now let's go ahead and set that up.
So I'm going to create a private static int next ID which I'm going to assign to the value of zero.
So this is a static field to hold the next ID.
Now the idea is that this is static right.
So it's not going to be part of each customer object.
It's somehow outside of each customer object.
Right.
So how do we now assign an ID for each customer.
Well what we're going to do is we can create a private read only.
That's the keyword here.
Read only int that I'm going to call underscore ID.
So this is going to be the unique identifier where it's different for every single user that we create.
And the idea is that even if you create, let's say five users and you delete the third one, it would
still go like this.
So you create a customer.
The customer has the ID of zero.
The next customer that you create has the ID of one, the next one of two.
So now let's say you delete the one with id two.
The next customer that you create will not get the two, but will get the three.
Okay, because every single customer entry will get a incremented ID.
So how can we now increment the ID and assign it accordingly to our ID of our customer?
Well, we can go into the default constructor here, and we can just say the underscore id is going
to be next ID plus plus.
So this plus plus will make sure that it's incremented.
So we are assigning zero and then we're incrementing it okay.
So that's what this plus plus after next ID does.
We looked at incrementing earlier in the course.
So now we mustn't forget to use the same thing in all of our constructors.
So here inside of our other constructor that we have.
So this is the constructor our custom constructor here okay.
We mustn't forget it there.
And we don't have any other constructors so that is fine.
So the idea here is that we are setting the ID and we cannot override the ID for any given customer
after the fact because it is read only.
So this variable here, this read only instance field can be initialized only once.
So it can only get assigned a value once, and after that it cannot get assigned the value anymore.
So what we're doing is we're assigning the value inside of and either this constructor or this constructor.
So they cannot be both called after another.
Only one of them will be called okay.
So either this one or this one in our case because we have two constructors.
If we had five, we would have to add this line in every single one of them.
So by doing that now, every single customer will get a unique ID.
Okay.
So now this ID is private and the only way we can actually see it is inside of this class.
Now we could get the details about the user.
So we could say something like public void, read details or get details would also be an option for
a name.
Either way is fine.
Get details or even write details right?
That would be fine as well.
So here I'm going to just say Console.WriteLine and details about the customer.
And then we can use like this.
So we're going to use the name name is and then and ID is.
And then we're going to use underscore ID here.
So it is a read only and it is private.
So it's only available inside of this particular class.
So this get details method can.
And get the ID.
Okay.
So now let's go over to our program CSS file.
And let's go ahead and create a customer.
So customer customer one new customer.
And now we're going to create another customer customer customer two.
And this customer two will now get some details.
So we know that there are multiple.
When you hover over it it says multiple overloads.
So overload plus one which means that there are multiple constructors that we can use.
So here we can either use the default constructor or we can add details like John Doe.
And then we can add the address.
But we don't have to okay.
The address and the contact number are fine by default.
And now we can use customer one dot get details and we can use customer two get details.
And now let's see what we're going to get from it okay.
Because you will see that we get the ID.
So the first customer has the ID of zero and the second customer has the ID of one.
Now if we were to create a third customer and a fourth customer and so forth, they would all just get
the next increment of our ID.
So let's go ahead and get details about customer three.
Get details that we are and let's run it.
And there we are details about customer name is customer and ID is two okay.
So that is something where we are shielding the ID entirely.
And it can only be accessed within the class.
But we can create methods that will expose it to outside but not the variable itself, but but only
the variable value.
Okay.
We cannot expose the variable itself, but only the value of the variable.
Okay, so that's the read only keyword for you.
And that's your unique identifier ID which is incremented each time that we are creating a new customer.
Because this static variable doesn't need an object of customer to exist, but it is a variable that
exists outside of the customer object, so to speak.
Okay, so that's it for this video.
In the next one I want to show you how you can also create read only properties.
All right.
So see you there.