newbie question

  • Thread starter Thread starter csharpnb
  • Start date Start date
C

csharpnb

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 said:
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?
 
csharpnb said:
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.
 
Jon Skeet said:
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.

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?
 
csharpnb said:
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?
 
Jon Skeet said:
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?

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