Interviews20 answers
How constants and read-only variables are different?
20 Answers
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
Komara ReddyPosted Oct 13, 2016, 1:33 PM EDT
In Constants variable is fixed value & Read only you can change value of at Run Time.
Nilesh PatelPosted Sep 19, 2016, 4:23 AM EDT
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.
Sandeep SinghPosted Aug 18, 2016, 7:21 AM EDT
Constants are assigned design time and read-only assigned run time.
Vishal JadavPosted Aug 6, 2016, 3:49 AM EDT
We can not change constant value. We can change read-only value in constructor
Mangesh KulkarniPosted Jul 31, 2016, 5:54 AM EDT
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.
Pooja SinghPosted Jul 6, 2016, 12:04 PM EDT
constant = cant change the value of the variable at the runtime
read-only = can change the value of the variable at the runtime
sakthi smithPosted Jun 26, 2016, 11:26 PM EDT
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.
Keerthi VenkatesanPosted Jun 7, 2016, 10:29 PM EDT
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
kapil kumarPosted Jun 7, 2016, 8:16 AM EDT
Const must be initialized at declaration time,
readonly can be initialized on the constructor
Bhuvanesh MohankumarPosted May 29, 2016, 7:22 AM EDT
Constants =Compile Time.
Read-Only=Run Time.
Thiruppathi RPosted May 27, 2016, 9:41 AM EDT
constants doen't change the values during the compile time so it is called as a read-only variable
Prasanna MuraliPosted May 20, 2016, 5:27 PM EDT
Constant variables must be assigned at compile time.
Read-only variables can be assigned at compile time or run time.
Vivek KumarPosted Apr 17, 2016, 1:52 AM EDT
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 Readonly
We 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);
}
}
Nayeem MansooriPosted Mar 22, 2016, 1:23 AM EDT
Constant variables should be assigned at compile time.
Read-only variables can be assigned at runtime.
Sreekanth ReddyPosted Mar 15, 2016, 1:44 AM EDT
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
Pankaj Kumar ChoudharyPosted Feb 9, 2016, 4:27 PM EST
Read only can be assigned in Constructor ...but u can't do the same with constants....
Nishant MittalPosted Feb 8, 2016, 7:06 PM EST
Constant is compile time variables and Readonly is runtime variable
Akhil GargPosted Jan 31, 2016, 2:25 AM EST
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.
Rajeev PunhaniPosted Jan 27, 2016, 8:00 AM EST
Constant is compile time variables whereas Readonly is runtime variable.
Sujeet SumanPosted Jan 27, 2016, 12:00 AM EST
















