What is the difference between const and static readonly?
-
Tuesday, April 07, 2009 8:06 AM
What is the difference between const and static readonly?
Please remember to mark the replies as answers if they help and unmark them if they provide no help.
Welcome to the All-In-One Code Framework! If you have any feedback, please tell us.
All Replies
-
Tuesday, April 07, 2009 8:12 AM
The difference is that the value of a static readonly field is set at run time, so it can have a different value for different executions of the program. However, the value of a const field is set to a compile time constant.
Remember:
For reference types, in both cases (static and instance), the readonly modifier only prevents you from assigning a new reference to the field. It specifically does not make immutable the object pointed to by the reference.For details, please refer to C# Frequently Asked Questions on this topic:
http://blogs.msdn.com/csharpfaq/archive/2004/12/03/274791.aspxRelated thread:
http://social.msdn.microsoft.com/forums/en-US/csharpgeneral/thread/16aff942-96fe-4a40-aa81-76d3add9cc19
http://social.msdn.microsoft.com/Forums/en-US/csharpgeneral/thread/ef3b8a65-315b-41de-80a8-d1f03a66f664/For more FAQ about Visual C# General, please see Visual C# General FAQ
Please remember to mark the replies as answers if they help and unmark them if they provide no help.
Welcome to the All-In-One Code Framework! If you have any feedback, please tell us.- Marked As Answer by Xiaoyun Li – MSFT Tuesday, April 07, 2009 8:13 AM
- Unmarked As Answer by Xiaoyun Li – MSFT Tuesday, April 07, 2009 10:25 AM
- Edited by Xiaoyun Li – MSFT Tuesday, April 07, 2009 10:25 AM
- Marked As Answer by Xiaoyun Li – MSFT Tuesday, April 07, 2009 10:25 AM
- Edited by Xiaoyun Li – MSFT Wednesday, April 08, 2009 2:32 AM
- Edited by Xiaoyun Li – MSFT Wednesday, April 08, 2009 2:33 AM
-
Tuesday, April 07, 2009 8:14 AMsame person asks the questions and gives the answer too???
Thanks and Regards,
Ganesh Ranganathan
Bangalore, India
MCTS - .NET Framework 2.0 Web Applications
P.S: Please mark the post as answer if you find it helpful -
Friday, April 10, 2009 5:59 AMModerator
Hello,
Please understand that this thread is one of the C# General FAQs.
For more C# General FAQ, please see
http://social.msdn.microsoft.com/Forums/en-US/csharpgeneral/thread/2d666562-ed08-4461-bf92-7808913b4e96Best Regards,
Lingzhi
Please remember to mark the replies as answers if they help and unmark them if they provide no help.
Welcome to the All-In-One Code Framework! If you have any feedback, please tell us. -
Thursday, April 16, 2009 8:02 AMModeratorPlease understand that this creates an impression of recognition point spiking among other contributors. Post these kind of threads as a "Discussion" type of thread. Locking this thread also creates a really bad impression.
An effective format is the one used by your colleague Rong-Chun Zhang. Check out his FAQ in the Windows Forms General forum.
Hans Passant. -
Thursday, April 16, 2009 8:54 AMModerator
Hi nobugz,
Please understand that we are in this forum for helping more community members, instead of for the points. Besides, answering our own questions won’t earn any points in this forum.
The question-typed thread has a higher priority in this forum’s search engine than the discussion-typed thread. In order to make the FAQ threads be much easier to access by the forum customers, we use the Question instead of the General Discusstion as the FAQ format.
If you have any further questions, please be free to contact me via v-micsun@microsoft.online.com (remove “online”).
Best Regards,
Lingzhi
Please remember to mark the replies as answers if they help and unmark them if they provide no help.
Welcome to the All-In-One Code Framework! If you have any feedback, please tell us. -
Thursday, April 16, 2009 9:12 AMModeratorHi Linghzi,
Understood, this is all about "impression". The current forum software is just not very suitable for what Xiaoyun is trying to accomplish. That's why I recommended Rong-Chun Zhang's approach. With 20,541 views, it has been working very well.
Hans Passant. -
Thursday, April 16, 2009 9:48 AMModerator
Hi nobugz,
Thank you very much for your suggestion!
We are always doing our best to improve our supporing service in MSDN Forums, so that we can help more community members. Currently, we are considering adding some explanation before the FAQ thread title, for example:
[FAQ – No Need to Reply] What is the difference between const and static readonly?
I think it can make the FAQ threads clearer for all of us to understand.
Best Regards,
Lingzhi
Please remember to mark the replies as answers if they help and unmark them if they provide no help.
Welcome to the All-In-One Code Framework! If you have any feedback, please tell us. -
Thursday, April 16, 2009 10:37 AMModeratorHi Lingzhi,
Maybe it is easier when we tackle this from the other end: what exactly is wrong with Rong-Chun Zhang's approach?
Hans Passant. -
Thursday, April 16, 2009 10:52 AMWell, I'm fairly new to this forum (read: fresh perspective :)) and I'd definitely prefer Rong-Chun Zhang's way.
Much less noise. -
Tuesday, August 31, 2010 3:19 PM
Hi,
Constant fields are used to set the value which are never changing in programe. Const fields need to be defined at compile time ie. at the time of coding by the developer. Once there values areassigned we could not able to change them. Whereas ReadOnly fields value can be set at run time. They are basicaly used inside the constructor body so that we can provide their value at run time.
- Proposed As Answer by Neeru Verma Tuesday, August 31, 2010 3:19 PM
-
Monday, February 20, 2012 6:31 AM
A Constant or a read only member can't be modified out side of class.
The basic differenc between them is
Constants:
1.These can't be modified with in a class or out of the class.
2.Intializing constants at the time of declaration was mandatory.
3.Constant members behave as a static member,which can be acessed through class name.
4.Memory allocation can be taken only once.
5.Object of the class is not required for intialization.
Read Only:
1.These can't be modified out of a class,but can be modified with in a class.
2.Intializing these at the time of declaration is optional,they can be intialized through constructor.
3.These behave as instance members which can b accessed only through object.
4.Memory allocation of these gets performed separately for each object of the class.
5.Object of the class is required for intialization.
-
Thursday, May 17, 2012 4:23 AMCan't we make the readonly fields static and access those using class name?
Thanks and Regards, Anil.
-
Monday, February 25, 2013 5:51 AMConst allows us to define compile-time constant values. Whereas, readonly fields are looked up at run-time.

