473,396 Members | 2,081 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

beginner's question: const vs readonly

Hello,
I am learning C#.NET and I donst understand difference between meaning
and usage of "readonly" and "const" keywords. Could you explain me
please.
Thank you
/RAM/
Jun 23 '06 #1
3 1491
Const expressions are evaluated at compile time for all assemblies using
them

Readonly expressions are evaluated at runtime either in the initializer
(readonly int 5 = {initializer};), or in the constructor; after the
constructor they cannot be changed. This can be useful to avoid accidental
updates to the field, or where the "const" expression can only be evaluated
at runtime - e.g. from a database of config file.

Note (more abstract) also that if assembly A references B, and B has a const
that A uses, then swapping B with an updated const value will *not* reflect
in A, as A's value is compiled already; with consts it *will* show sooner,
but it needs a runtime eval (once).

Also - you can have readonly fields at both the static and instance level,
but consts are (essentially) static. As an example of a readonly instance
field that varies per instance, consider the decorator / facade wrapper
pattern that provides alternative access to the members of an existing
object (e.g. the following that converts methods on the base into properties
on the decorator) - it rarely makes sense to change the base object of such
a wrapper, so it can be readonly.

public class SomeDecorator {
private readonly SomeOtherClass _base;
public SomeDecorator(SomeOtherClass base) {
_base = base;
}
public string SomeProperty {
get {return _base.SomeComplexMethod();}
set {_base.UpdateSomething(value);}
}
}
Jun 23 '06 #2
"Marc Gravell" <ma**********@gmail.com> wrote in message
news:Od**************@TK2MSFTNGP05.phx.gbl...
Note (more abstract) also that if assembly A references B, and B has a
const that A uses, then swapping B with an updated const value will *not*
reflect in A, as A's value is compiled already; with consts it *will* show
sooner, but it needs a runtime eval (once).


Interesting. (And you mean readonly in the last sentence, I assume.)

///ark
Jun 23 '06 #3
Yes, I meant readonly (a public field or property); my bad...

Marc

Jun 24 '06 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

19
by: cody | last post by:
Iam wondering what the benefit of using const over static readonly is. static readonly is a runtime constant and can be set once in the initializer or the static ctor, whereas const is suffering...
5
by: rh | last post by:
I created a user control that is made up of a TreeView and a VScrollBar. I set the TreeView.FullRowSelect = True and it works as expected (full row is visible, appears on top of everything else)...
2
by: Robert Smith jr. | last post by:
Hello, Please pardon my newbie question ... I am building an ASP.NET page that displays a recordset with a Delete statement enabled (this all works fine). I want to Insert the current row...
2
by: Amit | last post by:
Hello, I have a class where I store a lot of setting names. Right now they're all declared as consts : public const CurDoc as string = "CURRENT_DOCUMENT" From what I know about const, every...
4
by: R.A.M. | last post by:
When to use "readonly" and when to use "const"? Could you explain me please? Thank you very much! /RAM/
5
by: Sek | last post by:
hi folks, i have a bunch of strings used in my code in many places. these strings reside inside a instantiable class. so, i want to replace these with constant/static variable to control the...
2
by: wizofaus | last post by:
Given the following code: public class Test { static unsafe void StringManip(string data) { fixed (char* ps = data) ps = '$'; }
0
by: ChopStickr | last post by:
I have a custom control that is embedded (using the object tag) in an html document. The control takes a path to a local client ini file. Reads the file. Executes the program specified in...
3
by: count1986 | last post by:
Why C# has "readonly field" and "const value", while Java has only "final"? Are there any reasons for doing so?
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.