In this video you will learn how the const keyword works, as well as the read only keyword and how
they compare to each other.
So both of them allow you to create a variable that is read only and that is immutable.
So they are non modifiable fields, but they differ in terms of when they are initialized and their
usage contexts.
So let's look at both of them in order to fully understand them.
So let's say I have a public const int number of corners.
So a rectangle I'm in the rectangle class here has four corners.
And that is constant.
That will always be the same no matter which rectangle I make it's going to have four corners.
Now on the other hand I can have something that will be a public read only string color.
And the color of the rectangle in my case has to be either initialized during declaration or in the
constructor.
So this here is the declaration of the field.
Now the same goes for this here.
Now a const needs to be initialized during compile time.
So it needs to get a value during compile time, which means directly in the code in the line where
it is created.
So you cannot use a method here that would assign a value to it for the read only keyword.
It's fine if it is only assigned during runtime, so when it is initialized only during runtime, which
means it will get a value assigned to it during runtime.
So that's when we are playing the app or clicking on the run button.
So when would I initialize the color?
Well, I can do it in a constructor.
So let me quickly create a constructor here.
So the rectangle and I'm going to say each time that we create a new rectangle, we need to assign a
color.
And the color that will be assigned to my rectangle will be the color of that particular rectangle.
However, every single rectangle will have four corners.
So that is also a difference between const and read only.
So const is going to be the same for all instances of a class.
However, a read only variable will be different or can be different for every single instance of that
class.
So let's have a look at how this is actually going to pan out.
So I have created this method display details.
And what it will do is it will display the details about a rectangle.
So let's go ahead and now create a few rectangles in our program CSS file.
So here I'm going to create a rectangle which I'm going to call rectangle one.
And that will be a new rectangle.
And I need to assign a color here.
So I'm going to say something like red.
And then I'm going to create another rectangle that will be blue okay.
So rectangle two will be blue.
So now what I'm going to do is I'm going to use rectangle one dot display details and rectangle two
display details.
And now if we run this we will see the difference between them.
So we will see that they differ in color but they have the same amount of corners.
And that is really one of the major points of the const versus read only keywords.
So this is a constant that is going to be the same for every rectangle that we're going to create.
And this read only will be different or can be different for every single rectangle that we're creating
because we're only initializing it.
So assigning a value to it at the moment that we're creating a rectangle.
So in the constructor.
So let's try and override the color somewhere else.
So for example here let's try and assign a value like gray in here.
You see it doesn't work.
So what it says here a read only field cannot be assigned to except in a constructor or init only setter
of the type in which the field is defined, or a variable initializer.
So there were a few keywords that we didn't cover yet, but you can see that you can only set a value
to it or assign it in the constructor or directly here.
So I could have said that by default all colors are blue, and then they will get a different value
during the creation of the actual rectangle.
So then another point to discuss is that you see that the const and the read only variables are used
with capital letters.
So number of corners as well as color start both with a capital letter.
So that's something that you need to consider when it comes to naming.
However, there is one thing that overrides the naming convention here.
And that is for example, if you have a read only field that is a.
Private field for that particular variable.
Then this.
Here you can see this ID needs an underscore and uses the lower letter.
That's because this naming convention here overrides the naming convention there, but only for the
case that it is private.
So that's it for this video.
See you in the next one.