What is the Difference between read only and constant
variables?

Answers were Sorted based on User's Feedback



What is the Difference between read only and constant variables?..

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

What is the Difference between read only and constant variables?..

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

What is the Difference between read only and constant variables?..

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

What is the Difference between read only and constant variables?..

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

What is the Difference between read only and constant variables?..

Answer / anil chauhan

All the answers is right.
But the last one is very right.

Is This Answer Correct ?    13 Yes 4 No

What is the Difference between read only and constant variables?..

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

What is the Difference between read only and constant variables?..

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

What is the Difference between read only and constant variables?..

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

What is the Difference between read only and constant variables?..

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

What is the Difference between read only and constant variables?..

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

Post New Answer

More C Sharp Interview Questions

What is Serialization and how do we implement (in real time scenario)

1 Answers  


What is master page in asp net c#?

0 Answers  


When you inherit a protected class-level variable, who is it available to?

5 Answers   CMC, IBM,


Difference between type constructor and instance constructor? What is static constructor, when it will be fired? And what is its use?

0 Answers  


What is the difference between readonly and constant in c#

2 Answers   Fulcrum Logic,






Define an array?

0 Answers  


Define a manifest in .net?

0 Answers  


What are Sealed Classes in C#?

27 Answers   DataPoint, Wipro,


What is selector c#?

0 Answers  


What is predicate c#?

0 Answers  


Why is ienumerable used?

0 Answers  


Can we have a non static member function in a base class to be override in derived with static modifier?

7 Answers   Wipro,


Categories