GeekInterview.com
  I am new, Sign me up!
 
GeekInterview.com  >  Interview Questions  >  Microsoft  >  C#
Go To First  |  Previous Question  |  Next Question 
 C#  |  Question 3 of 436    Print  

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).




  
Total Answers and Comments: 8 Last Update: April 07, 2009   
  
 Sponsored Links

 
 Best Rated Answer
Submitted by: Eddie Quiroz
 
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.

Above answer was rated as good by the following members:
velsankar, chrismontora, Wise Man
July 12, 2005 05:16:54   #1  
praveen        

RE: What is the difference between const and static read-only?
this article is very good thx
 
Is this answer useful? Yes | No
September 03, 2005 19:50:11   #2  
Eddie Quiroz        

RE: What is the difference between const and static read-only?
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.
 
Is this answer useful? Yes | NoAnswer is useful 3   Answer is not useful 0Overall Rating: +3    
November 07, 2005 02:06:04   #3  
rushijoshi Member Since: November 2005   Contribution: 1    

RE: What ...
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.
 
Is this answer useful? Yes | No
June 06, 2006 05:03:21   #4  
Jaiprakash M Bankolli        

RE: What ...
To add to what is said till nowConst are initilized at the compile timestatic read-only can be changed at the run timeRegardsJaiprakash
 
Is this answer useful? Yes | No
April 07, 2007 23:54:37   #5  
BlueXBlue        

RE: What ...
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

 
Is this answer useful? Yes | No
April 07, 2007 23:55:22   #6  
BlueXBlue Member Since: April 2007   Contribution: 1    

RE: What ...
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



 
Is this answer useful? Yes | No
May 07, 2008 01:14:55   #7  
ushalakshmi Member Since: April 2008   Contribution: 40    

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

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

 
Is this answer useful? Yes | No
April 07, 2009 08:50:20   #8  
chandra_123 Member Since: April 2009   Contribution: 6    

RE: What is the difference between const and static read-only?
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.
 
Is this answer useful? Yes | No


 
Go To Top


 Sponsored Links

 
About Us -  Privacy Policy -  Terms and Conditions -  Contact -  Ask Question -  Propose Category -  Site Updates 

Copyright © 2005 - 2009 GeekInterview.com. All Rights Reserved

Page copy protected against web site content infringement by Copyscape