Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / Languages / C# / February 2008

Tip: Looking for answers? Try searching our database.

const and readonly

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
TonyJ - 20 Feb 2008 07:35 GMT
Hello!!

Assume I have this class declaration of MyTest below.
I just want to check with you if the two declarations for tal1 and tal2 is
almost the same.
The only difference is that const is set at compile but readonly can be set
at compile time or run time appart from
that they are the same.
Both is actually a class field because const is always automatically static
so for me these two
decalations is almost the same appart from my comment above.
Do you agree?

class MyTest
{
   public static readonly int tal1 = 1;
   public const int tal2 = 1;
...
}

//Tony
Jon Skeet [C# MVP] - 20 Feb 2008 07:43 GMT
> Assume I have this class declaration of MyTest below.
> I just want to check with you if the two declarations for tal1 and tal2 is
[quoted text clipped - 13 lines]
> ...
> }

If the constants are only used within the same assembly, that's all
correct (const is limited to a select set of types, too).

Things are messier when you refer to the constant from a different
assembly. Whenever you refer to a const (whether in the same assembly
or not) it bakes the value of the const into the *referring* code -
instead of actually looking up the value of the field at execution
time.

That has a significant effect if you build different assemblies at
different times. Suppose you have Const.dll and PrintConst.exe which do
the obvious things. If Const.dll has a const with the value "First"
when you compile PrintConst.exe against it, then you change the const
in Const.dll to "Second" and *just* rebuild Const.dll, running
PrintConst.exe will still show "First" because the value has been
compiled into PrintConst.exe.

Does that make sense?

Signature

Jon Skeet - <skeet@pobox.com>
http://www.pobox.com/~skeet   Blog: http://www.msmvps.com/jon.skeet
World class .NET training in the UK: http://iterativetraining.co.uk

TonyJ - 20 Feb 2008 07:59 GMT
Hello!!

I understand but the solution for how .NET framework has solved it when
dealing with const between assemblies was not the best choice.

//Tony

> > Assume I have this class declaration of MyTest below.
> > I just want to check with you if the two declarations for tal1 and tal2 is
[quoted text clipped - 32 lines]
>
> Does that make sense?
Jon Skeet [C# MVP] - 20 Feb 2008 08:16 GMT
> I understand but the solution for how .NET framework has solved it when
> dealing with const between assemblies was not the best choice.

Well, it's useful occasionally - and easy to avoid, just by using
static readonly instead.

Signature

Jon Skeet - <skeet@pobox.com>
http://www.pobox.com/~skeet   Blog: http://www.msmvps.com/jon.skeet
World class .NET training in the UK: http://iterativetraining.co.uk

Lasse Vågsæther Karlsen - 20 Feb 2008 13:55 GMT
>> I understand but the solution for how .NET framework has solved it when
>> dealing with const between assemblies was not the best choice.
>
> Well, it's useful occasionally - and easy to avoid, just by using
> static readonly instead.

Not to split hairs or anything, but if it is constant, why is it
changing? :)

Signature

Lasse Vågsæther Karlsen
mailto:lasse@vkarlsen.no
http://presentationmode.blogspot.com/
PGP KeyID: 0xBCDEA2E3

Jon Skeet [C# MVP] - 20 Feb 2008 14:00 GMT
> > Well, it's useful occasionally - and easy to avoid, just by using
> > static readonly instead.
>
> Not to split hairs or anything, but if it is constant, why is it
> changing? :)

Because someone *thought* it was a constant with one value, but that
turned out to be wrong. Or because someone used "const" without really
thinking about whether or not it was genuinely a constant.

I'm not saying it should happen, just that it does and it's worth being
aware of the consequences :)

Signature

Jon Skeet - <skeet@pobox.com>
http://www.pobox.com/~skeet   Blog: http://www.msmvps.com/jon.skeet
World class .NET training in the UK: http://iterativetraining.co.uk


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.