By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
445,918 Members | 2,131 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 445,918 IT Pros & Developers. It's quick & easy.

Difference between const and readonly

P: n/a
Hello,
I have a class where I store a lot of setting names. Right now they're all
declared as consts :
public const CurDoc as string = "CURRENT_DOCUMENT"

From what I know about const, every call to CurDoc will be replaced with the
text "CURRENT_DOCUMENT" during compilation. Is this right? If yes then
wouldn't it be better to declare it as:
public shared readonly CurDoc as string = "CURRENT_DOCUMENT"

This way there'll be a reference to this string, instead of it being copied
at each access point. What's the best practice for these?
Thanks
Amit
Nov 21 '05 #1
Share this Question
Share on Google+
2 Replies


P: n/a
From what I know about const, every call to CurDoc will be replaced with the
text "CURRENT_DOCUMENT" during compilation. Is this right?
Sort of. There's no "calling" going on though.

This way there'll be a reference to this string, instead of it being copied
at each access point. What's the best practice for these?


That's what you'll get when using a const too, without the need for a
field access.

Mattias

--
Mattias Sjögren [MVP] mattias @ mvps.org
http://www.msjogren.net/dotnet/ | http://www.dotnetinterop.com
Please reply only to the newsgroup.
Nov 21 '05 #2

P: n/a
Amit,
In addition to the other comments:

The important distinction between Const & Readonly is when the field is
defined in another assembly.

For example:

' Project 1
public Const ConstDoc as string = "CURRENT_DOCUMENT"
public Shared Readonly ReadOnlyDoc as string = "CURRENT_DOCUMENT"
' Project 2
' references Project 1

When you use ConstDoc in Project 2, a copy of the literal is placed in
Project 2 assembly, if you recompile Project 1 without recompiling Project
2. Project 2 will continue to use the original value of ConstDoc.

Where as when you use ReadOnlyDoc in Project 2, Project 2 will ask Project 1
for the value at runtime, allowing you to freely recompile Project 1 without
recompiling Project 2.

Also Const will be evaluated (known) at compile time, where as Readonly can
be computed at run time.

Another important difference is that Const are implicitly Shared, where as
Readonly can be either a Shared or instance field. For example
EventArgs.Empty is an example of a Shared Readonly field, I will use Shared
instance fields for fields that are not known until I construct the object,
however I want to ensure the value of the field never changes...

Hope this helps
Jay

"Amit" <re***********@hotmail.com> wrote in message
news:%2****************@TK2MSFTNGP14.phx.gbl... Hello,
I have a class where I store a lot of setting names. Right now they're all
declared as consts :
public const CurDoc as string = "CURRENT_DOCUMENT"

From what I know about const, every call to CurDoc will be replaced with
the
text "CURRENT_DOCUMENT" during compilation. Is this right? If yes then
wouldn't it be better to declare it as:
public shared readonly CurDoc as string = "CURRENT_DOCUMENT"

This way there'll be a reference to this string, instead of it being
copied
at each access point. What's the best practice for these?
Thanks
Amit

Nov 21 '05 #3

This discussion thread is closed

Replies have been disabled for this discussion.