Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

difference between ReadOnly and Const? [duplicate]

Tags:

c#

Possible Duplicate:
What is the difference between const and readonly?

are these interchangeable? can you show me code on how you would apply the two?

like image 313
Alex Gordon Avatar asked Oct 26 '25 15:10

Alex Gordon


1 Answers

No, they aren't.

A const field is literal value embedded in the assembly.
Only primitive values (strings and numbers) can be const, and they are evaluated at compile time.
When you reference a const field, the compiler embeds the literal value of the field. Therefore, if use use a const from another assembly, and the other assembly is recompiled with a different value, your assembly will only use the new value if you recompile it against the new version.


A readonly field is a normal field that cannot be changed outside the constructor.

like image 50
SLaks Avatar answered Oct 29 '25 05:10

SLaks



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!