What is the difference between: readonly and const

  • Thread starter My4thPersonality
  • Start date
M

My4thPersonality

I am reading something about the details of C#, and I came acros the
statements readonly and const. I do not understand, it seems to be the same,
what's the difference? Here is the text were it refers to:

const long size = ((long)int.MaxValue + 1) / 4;

Constant-Value must be computable at compile time

readonly DateTime date;

Read Only Field-Must be initialized in their declaration or in a
constructor-Value needs not be computable at compile time-Consumes a memory
location (like a field)

(Or it should be the latter, that const is created on the stack and readonly
on the heap??)
 
M

Morten Wennevik

Hi My4thPersonality,

const needs to be set at compile time while readonly can be generated
dynamically in a constructor, but you can set it only once.

readonly int readonlyInt;
const int constint = 0;

public MyClass(int n)
{
readonlyInt = n;
}
 
J

Jon Skeet [C# MVP]

My4thPersonality said:
I am reading something about the details of C#, and I came acros the
statements readonly and const. I do not understand, it seems to be the same,
what's the difference? Here is the text were it refers to:

const long size = ((long)int.MaxValue + 1) / 4;

Constant-Value must be computable at compile time

readonly DateTime date;

Read Only Field-Must be initialized in their declaration or in a
constructor-Value needs not be computable at compile time-Consumes a memory
location (like a field)

(Or it should be the latter, that const is created on the stack and readonly
on the heap??)

Const is compiled directly into the code - if you write

const long Size = 100; // or whatever

then later

int x = Size;

it compiles the declaration/assignment of x as

int x = 100;

rather than as a run-time reference to the Size member.

It's not really got anything to do with stack/heap.
 
M

My4thPersonality

Morten Wennevik said:
const needs to be set at compile time while readonly can be generated
dynamically in a constructor, but you can set it only once.

Thanks for the quick answer!
 
S

Simon Shearn

My4thPersonality said:
I am reading something about the details of C#, and I came acros the
statements readonly and const. I do not understand, it seems to be the same,
what's the difference? Here is the text were it refers to:

const long size = ((long)int.MaxValue + 1) / 4;

Constant-Value must be computable at compile time

readonly DateTime date;

Read Only Field-Must be initialized in their declaration or in a
constructor-Value needs not be computable at compile time-Consumes a memory
location (like a field)

(Or it should be the latter, that const is created on the stack and readonly
on the heap??)

Hello -

I know of one difference regarding versioning - if you write a DLL in C#
that defines constants, then the values of those constants will be
hard-coded into applications that call the DLL at compile time, and the DLL
is not called at runtime when the constant is referenced. This causes
problems if you're not using strong versioning and you produce a new
version of the DLL containing different constant values and just
xcopy-deploy it without recompiling the calling application. readonly fields
don't suffer from this problem.

Regards,

Simon
 
M

My4thPersonality

Simon Shearn said:
I know of one difference regarding versioning - if you write a DLL in C# ....
don't suffer from this problem.

Was this actually the reason why they introduced the readonly variable? I
know C++, and I cannot think of a simular keyword for this in C++. Perhaps
it's just that my C++ knowlegde is not perfect, or it could be that the
readonly variable was convenient on the .NET platform especially, for this
versioning problem. (Just curious about this.)
 
G

Guest

readonly can be generated dynamically
in a constructor, but you can set it only once.

Here's a trivial point: readonly can be set multiple times, but it can only
be done with a variable initializer (inline) or within the constructor.
 
G

Guest

I am reading something about the details of C#, and I came acros the
statements readonly and const. I do not understand, it seems to be the same,
what's the difference?

This is actually a pretty complicated issue. Here are a couple of points
which might help you choose between them.

const is only useful for the predefined types like int, double, char, etc.
const has to be given a value at compile time and these are the only things
where you have a compile-time constant (17, 3.14, 'A', etc.).

const does work for string since we also have a literal for that (string s =
"hello";) but it is rarely done in practice since it applies to the reference
and not the object and the data inside a string is already immutable.

If you are using a struct that is not a predefined type (DateTime, TimeSpan,
etc.) then readonly is your only option. You can't use const since you don't
have a compile-time literal for them.
 
B

Bruno Jouhier [MVP]

My4thPersonality said:
Was this actually the reason why they introduced the readonly variable? I
know C++, and I cannot think of a simular keyword for this in C++. Perhaps
it's just that my C++ knowlegde is not perfect, or it could be that the
readonly variable was convenient on the .NET platform especially, for this
versioning problem. (Just curious about this.)

In Java (C# is strongly inspired from Java), you can qualify a field with
"final" and the semantics are the same as when you qualify with "readonly"
in C#. On the other hand, Java does not have any "const" keywork. So, C#
borrowed const from C++ and readonly from Java!

Bruno.
 
J

Jon Skeet [C# MVP]

MarkT said:
This is actually a pretty complicated issue. Here are a couple of points
which might help you choose between them.

const is only useful for the predefined types like int, double, char, etc.
const has to be given a value at compile time and these are the only things
where you have a compile-time constant (17, 3.14, 'A', etc.).

const does work for string since we also have a literal for that (string s =
"hello";) but it is rarely done in practice since it applies to the reference
and not the object and the data inside a string is already immutable.

The same would be true for all the primitive types though. I find that
const strings are quite common, actually. Other than versioning
reasons, there are few reasons to make a string variable readonly
instead of const in cases where you can use const.
 
M

My4thPersonality

Bruno Jouhier said:
In Java (C# is strongly inspired from Java), you can qualify a field with
"final" and the semantics are the same as when you qualify with "readonly"
in C#.

I thought you could compare the keyword sealed in C# to final in Java? Or is
that valid for classes and virtual functions, and you can compare final for
a field with readonly?
 
B

Bruno Jouhier [MVP]

My4thPersonality said:
I thought you could compare the keyword sealed in C# to final in Java? Or
is that valid for classes and virtual functions, and you can compare final
for a field with readonly?

Yes, final has many usages in Java
final applied to class or method corresponds to sealed in C#
final applied to field corresponds to readonly in C#

And Java also lets you apply final to a local variable, in which case, it
corresponds more or less to C++'s const. For example:
final int foo = some_expression; // local variable declaration, foo
cannot be reassigned.
 
M

My4thPersonality

Bruno Jouhier said:
Yes, final has many usages in Java
.....

Thanks! I think learning Java, C#, and comparing it to C++ really learns me
a lot and gives me opportunities, so I must say this ng and your answers are
helping me a lot!!
 
G

Guest

Yes, final has many usages in Java

there's one more: final parameter in Java (which mostly gets used with
anonymous classes).
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top