How constants and read-only variables are different?
Ayyaz Ahmad
Constants =Compile Time. Read-Only=Run Time.
Constant variable is a fixed value for the whole class where as Read-Only variable is a fixed value specific to a particular Instance of the Class.
const: Can't be changed anywhere.readonly: This value can only be changed in the constructor. Can't be changed in normal functions.Example : Below my example which is show diffence between Const and ReadonlyWe have a Test Class in which we have two variables one is readonly and another is constant.class Test { readonly int read = 10; const int cons = 10; public Test() { read = 100; cons = 100; } public void Check() { Console.WriteLine("Read only : {0}", read); Console.WriteLine("const : {0}", cons); } } Here I was trying to change the value of both the variables in constructor but when I am trying to change the constant it gives an error to change their value in that block which have to call at run time. So finally remove that line of code from class and call this Check() function like the following code class Program { static void Main(string[] args) { Test obj = new Test(); obj.Check(); Console.ReadLine(); } } class Test { readonly int read = 10; const int cons = 10; public Test() { read = 100; } public void Check() { Console.WriteLine("Read only : {0}", read); Console.WriteLine("const : {0}", cons); } }
we need to assign a value to constant variables at run time and it is constant through out the application,in case read-only we can assign a value at run time after that it is constant through out the application
In Constants variable is fixed value & Read only you can change value of at Run Time.
Constants are assigned design time and read-only assigned run time.
We can not change constant value. We can change read-only value in constructor
Constant =compiler Time, Read-only =run time. constant value is always constant but read -only value some time varies. example- constant= meterToCm , it is always constant. example- read-only=pie=3.1 but any other user written pie value is 3.14 then its varies value.
constant = cant change the value of the variable at the runtime read-only = can change the value of the variable at the runtime
Cnstant and ReadOnly keyword are used to make a field constant which value cannot be modified. Static keyword is used to make members static that can be shared by all the class objects.
you cant change the value of const after declaration but Read only variables can be assigned values either at runtime or at the time of instance initialization via constructor
Const must be initialized at declaration time, readonly can be initialized on the constructor
constants doen't change the values during the compile time so it is called as a read-only variable
Constant variables must be assigned at compile time. Read-only variables can be assigned at compile time or run time.
Constant variables should be assigned at compile time. Read-only variables can be assigned at runtime.
Constants are called compile time constant and read-only are called run time constant, Read only can be assigned in Constructor ...but u can't do the same with constants
Read only can be assigned in Constructor ...but u can't do the same with constants....
Constant is compile time variables and Readonly is runtime variable
Constants are the variables that are assigned value at compile time and they cannot be changed later.Read only variables can be initialised at the time of declaration or from constructor.
Constant is compile time variables whereas Readonly is runtime variable.