What is the Difference between read only and constant
variables?
Answers were Sorted based on User's Feedback
Answer / guest
Read only variable can be assigned a value at the runtime.
Where as const have to be assigned at the compile time only.
Is This Answer Correct ? | 64 Yes | 5 No |
Answer / parmjit
We can assign a value to a readonly variable at runtime
till it is a unassined identifier, but once value is
assigned, it cannot be changed.
In case of const we cannot assign a value at run time i.e.
the value assigned to it at declaration time cannot be
change
Is This Answer Correct ? | 39 Yes | 3 No |
Answer / kishore maddineni
all above answers are correct.But there is another point.
readonly variable can be assigned at declaration or at run
time which is must be in constructor. Readonly value can't
be modified.
Constant variable must be assigned at the time of
declaration only ,which is can't be modified.
Is This Answer Correct ? | 36 Yes | 7 No |
Answer / pankaj
Readonly variable can be assigned in run time but constant
variables cnt assign at run time.constant variables is
assigned at compile time only
Is This Answer Correct ? | 17 Yes | 3 No |
Answer / anil chauhan
All the answers is right.
But the last one is very right.
Is This Answer Correct ? | 13 Yes | 4 No |
Answer / raju
1.We can assign value to readonly field, only in
constructor or while declare the variable.But in case of
const we should assign a value while declare.
2.const=static readonly
Is This Answer Correct ? | 3 Yes | 1 No |
Answer / vijay bhaskar semwal
some more difference
Unlike const, when using readonly, the value is set when
the instance is created. This means that you can specify
the value of the readonly in the constructor of your class.
Another difference between const and readonly, is that
const are static by default, where with readonly, you must
define it as static (if you want it to be).
Is This Answer Correct ? | 2 Yes | 1 No |
Answer / shyam
Constant Value will be same across all Objects , Read Only variable can have different value in different object of class...
Is This Answer Correct ? | 0 Yes | 0 No |
Answer / dilip tiwari
For Eg I have used constant(public const int X = 123;
)in my assembly and compiled that assembly. After compiling
I haved used the .dll in myapplication then the value of X
is 123.After some time recognised that value of X is 234
then to make the changes I have to again recompile my
assembly and main application. But If I use redonly then I
have to just compile my assembly and the reference of new
dll in my main application. No need to recompile my main
application
using System;
public class A
{
public const int X = 123;
}
csc /t:library /out:A.dll A.cs
using System;
public class MyTest
{
public static void Main()
{
Console.WriteLine("X value = {0}", A.X);
}
}
csc /r:A.dll MyTest.cs
To run: mytest
The output :
X value = 123
Then you install the program into your client computer. It
runs perfectly.
One week later, you realised that the value of X should
have been 812 instead of 123.
What you will need to do is to
1] Compile A (after making the changes)
csc /t:library /out:A.dll A.cs
2] Compile your application again
csc /r:A.dll MyTest.cs
This can be a little troublesome. However, if you used the
readonly instead of const,the situation will be slightly
different. You start with
using System;
public class A
{
public static readonly int X = 123;
}
csc /t:library /out:A.dll A.cs
using System;
public class MyTest
{
public static void Main()
{
Console.WriteLine("X value = {0}", A.X);
}
}
csc /r:A.dll MyTest.cs
To run: mytest
The output :
X value = 123
Now you realised, you have made a mistake. All you need to
do is
1] Recompile A.cs (after making changes)
csc /t:library /out:A.dll A.cs
2] Copy the new dll to the client computer and it should
run perfectly. There is no need to recompile your
application MyTest.cs here
Is This Answer Correct ? | 0 Yes | 0 No |
Answer / akshay
Constants :
* Constant value. Cannot be changed once initialized.
Should be Initialize the value where it declared.
* Const value fully evaluated at compile time
* By default static
* Possible to declare within function
Readonly :
* Could be declare as Static or Non Static
* Assign the value where it declared or can be assigned in
constructor
* Could not declare within function
* can be initialized in compile time or in runtime.
Is This Answer Correct ? | 0 Yes | 0 No |
What is a framework in c#?
What is the use of return in c#?
what are the contents of an assembly ?
What are c# attributes and its significance?
How to parse a date time string?
What are the characteristics of c#?
Can you declare the override method static while the original method is non-static?
Define a manifest in .net?
explain Garbage collector’s functionality on unmanaged code
What are the advantages of generics in c#?
How long will it take to learn c sharp?
How does bitwise work?