Difference Between Constant and ReadOnly in C# | CSharp Interview Questions & Answers

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
in this video we will try to understand what's the difference between constant and read-only now both of these guys you know help you to achieve one common task and that is defining constants in your application so in order to understand the differences what we will do is first let's go ahead and you know let's create a sample example where we will use both read-only as well as constant and then we will try to figure out the differences between them so here's a simple console application we have and let's first go ahead and define a simple constant here so what I'll do is I'll go and define a constant now in order to define a constant we have to use the Const keyword so I'm going to say public Const int let's define a constant you know which is like centimeters to meters okay so that's 100 right and also let's go ahead and define a simple read only variable so read-only must be I'll just take one more example like pi 3.18 now if you watch very closely right you know both of these guys are constants in other words if I try to change them for example if I try to go and change this cm okay now in order to use these variables inside this static void main you know this guy you know these variables have to be static themselves so I'm going to just make them static right so now if I try to go and change this cm to meters to like 1 not 1 you can see that there is a red sign over here and say that it says that no we can't change this value and the same way if I try to go and change the PI over here like must be 3.14 if I try to do that again you can see that there are errors here saying that a static read-only field cannot be assigned in other words a read-only field cannot be changed okay so now again you know we are on the same question so what's the difference I mean to say if both of them are constants you know then why don't we just keep must be constant or just the read-only key but why we have two different variables and what is their use so now let me make a statement here the difference between a constant and read-only is that constant is a compile-time constant and read-only is a runtime constant okay now let us try to prove this statement what I've just spoken compile-time constant means they are absolute constant in other words you know the the value is set during the declaration itself and this value for example here we have this constant variable called as cm - meters this value 100 you know is baked inside the aisle code itself read-only constant means you know basically if you want if you wish you don't need to define this value during the declaration itself if you want you can go and set this value you know when your constructor fires or when your application initializes in other words if I wish I can go here and set this pi variable you know in the constructor so I can just say a program and I can set this PI over here 3.1 all right in simple words I will use constant you know when the data is absolutely absolutely constant in other words for example if you see here one meter will be 100 centimeter you know in space and time in any world in any state anywhere on the earth it's not going to change so for those kind of variables you know which is which are having absolute constant values I will use the Const keyword but now look at the other example that is pi now PI you know can change and change means you know like for example somebody would like to have a PI value of one precision so like 3.1 or somebody would like to have a PI value of 3.14 that is of two position right so it completely depends you know on scenario you know somebody wants a one precision PI value or somebody wants a to precision pi value but once the pi value is initialized it should never be changed in the application during runtime so for those kind of scenarios I will use read-only so constant I will use you know when it is absolutely constant and read only I will use you know you know during the run time you know I want to decide the value once and after that you know the value is always constant so so you can you can think about that no read-only values you will probably set it from the configuration file once for example like this pi value I will not write it in the program probably I will go here and create a you know some kind of what you call a app setting here saying PI value is equal to three point one four and then this value I would like to go and read in the constructor so I would like to go here and read the value from here and set it in the when the application initializes so I am going to say to double so in other words you know this pi value you know will get initialized from the app config file you know when the constructor runs the for the first time and afterwards in the application nobody can go and change the pi value so in other words I cannot go and do something like this now okay because you know that's not allowed right so in other words you know just retraining the same thing which I was speaking when I made the statement Const it is a compile-time constant and the read-only is a runtime constant now one more important thing is that this compile time constant is stored inside the IL code itself so in other words if you go and build the solution here and if you go and run your il DSM tools so if you go to your Visual Studio to your command prompt and if you just go to your IL DSM and if you go and open the exe the constant value is baked inside this code over here so if you go and open this sorry not the PI you can see now this constant is baked inside the IL code itself okay so the const value is stored inside the IL code itself it is a part of the IL code it is a compile-time thing while if you go and see you know the pi value this gets initialized in the constructor here so you can see that pi gets initialized you know from the wari call from name value collection or you know from the from the from the app config file okay so the difference between both of them is that you know one you will use for absolute constant and the other one you will use you know when it's more of like a semi constant you know you initialize it at one time and then afterwards it remains constant in the application so I hope that you enjoyed this video in this video we were trying to understand what's the difference between a Const keyword and the read-only keyword in c-sharp thank you so much
Info
Channel: Questpond
Views: 198,944
Rating: undefined out of 5
Keywords: c#, .NET, Constant, Const, Readonly, visual studio, csharp, Learn .NET, Step By Step .NET, Learn C#, Step By Step C#
Id: vNPBWNNlDwM
Channel Id: undefined
Length: 7min 33sec (453 seconds)
Published: Sun Apr 22 2012
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.