What is the difference between const and static read-only?

The difference is that static read-only can be modified by the containing class, but const can never be modified and must be initialized to a compile time constant. To expand on the static read-only case a bit, the containing class can only modify it:

-- in the variable declaration (through a variable initializer).

-- in the static constructor (instance constructors if it's not static).

Editorial / Best Answer

Answered by: Eddie Quiroz

  • Sep 3rd, 2005


A const must be initialized at the time of its creation. A readonly field can be assigned to once in the class constructor allowing you to pass in the value at run-time. Declaring fields as const protects both you and other programmers from accidentally changing the value of the field. Also note that with const fields, the compiler performs some optimization by not declaring any stack space for the field. The readonly keyword is similar to const, with two exceptions. First, the storage of a readonly field is the same as a regular read-write field, and thus there is no performance benefit. Secondly, readonly fields can be initialized in the constructor of the containing class.

Showing Answers 1 - 19 of 19 Answers

praveen

  • Jul 12th, 2005
 

this article is very good,thx

  Was this answer useful?  Yes

Eddie Quiroz

  • Sep 3rd, 2005
 

A const must be initialized at the time of its creation. A readonly field can be assigned to once in the class constructor allowing you to pass in the value at run-time. Declaring fields as const protects both you and other programmers from accidentally changing the value of the field. Also note that with const fields, the compiler performs some optimization by not declaring any stack space for the field. The readonly keyword is similar to const, with two exceptions. First, the storage of a readonly field is the same as a regular read-write field, and thus there is no performance benefit. Secondly, readonly fields can be initialized in the constructor of the containing class.

rushijoshi

  • Nov 7th, 2005
 

Readonly also usefull to give a constant value which is constructor independent.Constant is initialize at compile time where Readonly is initiaize at runtime (but from constructor only).Now Static Read-only can't be initialize from any normal (private, public, protected) constructor or any method. To initialize Static read-only u have to create a Static Constructor of that class.So there is no any major difference betn Const and Static Read-only...identically both r same for access level except their storage location is difference.

Jaiprakash M Bankolli

  • Jun 6th, 2006
 

To add to what is said till nowConst are initilized at the compile timestatic read-only can be changed at the run timeRegardsJaiprakash

  Was this answer useful?  Yes

BlueXBlue

  • Apr 7th, 2007
 

about ReadOnly ..

can any one give me the benefit of the ability of initializing the Readonly variable more than once?

coz its special as i know in C# only ..

thx in advance

  Was this answer useful?  Yes

BlueXBlue

  • Apr 7th, 2007
 

about ReadOnly ..

can any one give me the benefit of the ability of initializing the Readonly variable more than once in the Ctor ?

coz its special as i know in C# only ..

thx in advance


  Was this answer useful?  Yes


Constant : The constants are the one whose value remain same at all the time.
it will be used if u want to define something at compile time.

Read only....

if you don't know value at compile time but u can find that at runtime that time u can use readonly ..
 
Read only are the things which are not to allowed to alter by the user but it can be altered by itself.

Like the path of the application exe is read only. If you copy exe to some other directory, the path will change. But still it is rad only

  Was this answer useful?  Yes

As static read-only variables must be initialized in the static constructor (static constructor cannot have parameters and it cannot be called manually), it is efficient to used const variables over static read-only variables if you know the values at creation of these variables.

  Was this answer useful?  Yes

The Constant Fields are those which cannot be changed in runtime. Its value has to be assigned at declaration time.
const string strMyString="Constant Data" ; //This is valid.
Once declared now the value cannot be reassigned
strMyString="Changed Data" ; //Not Allowed.
This type of fields are required when one of the field values remains constant through out the system.
Example the value of Pi will remain same in your Maths Class.

This type of fields are implicitly static . This means that we access Constant data at class level.

The Readonly fields are similar to Constant field.
The only difference that the values can be assigned at runtime and that too in constructors.
Also this field are not implicit static field. Therefore they has to be accessed through instance level.
This find suitable when we dont know the value at compile time but the value is known only at run time.

Static Readonly fields are similar to Readonly field with only difference that the values can be assigned in static constructor only

  Was this answer useful?  Yes

Give your answer:

If you think the above answer is not correct, Please select a reason and add your answer below.

 

Related Answered Questions

 

Related Open Questions