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