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# / December 2005

Tip: Looking for answers? Try searching our database.

newbie question

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
csharpnb - 22 Dec 2005 05:56 GMT
Hi. Can anyone confirm please. Basically, the difference between const and
readonly is that const fields need to be known at compile time, whereas
readonly fields are unchangeable fields that can be initialized at runtime.
Am I getting this right?
csharpnb - 22 Dec 2005 06:00 GMT
> Hi. Can anyone confirm please. Basically, the difference between const and
> readonly is that const fields need to be known at compile time, whereas
> readonly fields are unchangeable fields that can be initialized at
> runtime. Am I getting this right?

Forgot to add that const fiels are implicitly static, whereas readonly
fields are not. Am I ok so far?
Andrew Robinson - 22 Dec 2005 06:30 GMT
Readonly fields can be assigned in the constructor of a class.

Signature

Andrew Robinson
www.binaryocean.com
www.bellinghamdotnet.org

>> Hi. Can anyone confirm please. Basically, the difference between const
>> and readonly is that const fields need to be known at compile time,
[quoted text clipped - 3 lines]
> Forgot to add that const fiels are implicitly static, whereas readonly
> fields are not. Am I ok so far?
Jon Skeet [C# MVP] - 22 Dec 2005 06:37 GMT
> > Hi. Can anyone confirm please. Basically, the difference between const and
> > readonly is that const fields need to be known at compile time, whereas
[quoted text clipped - 3 lines]
> Forgot to add that const fiels are implicitly static, whereas readonly
> fields are not. Am I ok so far?

Yes. There's an important difference you've missed though - if a class
in one assembly refers to a const in another assembly, the value is
"baked" into the first assembly. That means if you change the const
value, you need to recompile both assemblies. With a readonly field,
the value is fetched at runtime - that's slightly slower, but allows
you to change the value later (in your source code, I mean) without
recompiling everything that uses it.

Signature

Jon Skeet - <skeet@pobox.com>
http://www.pobox.com/~skeet   Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too

csharpnb - 22 Dec 2005 07:01 GMT
>> > Hi. Can anyone confirm please. Basically, the difference between const
>> > and
[quoted text clipped - 12 lines]
> you to change the value later (in your source code, I mean) without
> recompiling everything that uses it.

Thanks guys for the replies.
Jon, I can't seem to make a point of this "backing" action. Could you please
provide a quick example I can experiment with?
Jon Skeet [C# MVP] - 22 Dec 2005 07:14 GMT
> Thanks guys for the replies.
> Jon, I can't seem to make a point of this "backing" action. Could you please
> provide a quick example I can experiment with?

Sure:

ClassLib.cs:
public class ClassLib
{
   public const int Const = 1;
   public static readonly int ReadOnly = 1;
}

Test.cs:
using System;

class Test
{
   static void Main()
   {
       Console.WriteLine ("Const: {0}", ClassLib.Const);
       Console.WriteLine ("ReadOnly: {0}", ClassLib.ReadOnly);
   }
}

Compile and run:
csc /target:library ClassLib.cs
csc /r:ClassLib.dll Test.cs
test

Output:
Const: 1
ReadOnly: 1

Now change both values in ClassLib.cs to 2, and recompile *just
classlib*.

Rerun the test, and you'll get:

Const: 1
ReadOnly: 2

The value of Const hasn't been updated, because when Test.exe was
compiled, the value was copied straight into the executable - at
runtime, it isn't being fetched from ClassLib (which it is for the
readonly field).

Does that help?

Signature

Jon Skeet - <skeet@pobox.com>
http://www.pobox.com/~skeet   Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too

csharpnb - 22 Dec 2005 07:56 GMT
>> Thanks guys for the replies.
>> Jon, I can't seem to make a point of this "backing" action. Could you
[quoted text clipped - 45 lines]
>
> Does that help?

Yes, it's all clear now. Thank you.

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.