473,385 Members | 1,542 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,385 software developers and data experts.

const or readonly

Hi,

can someone please enlighten me on the difference between a "const" and a
"readonly" declaration?

TIA,
Axel Dahmen
Nov 17 '05 #1
2 1262
Axel Dahmen <NO*****@NoOneKnows.invalid> wrote:
can someone please enlighten me on the difference between a "const" and a
"readonly" declaration?


If a field is "const", anything which refers to the field has the value
of the constant placed into the IL - there is no runtime reference to
it. This has one potentially nasty side-effect - if you change the
value of a const field in one assembly (at compile-time, I mean) then
any other assemblies which need to see the "new" value need to be
recompiled - they won't try to "get" the value from the originating
assembly at runtime.

Consts are probably more performant (although it's unlikely to be
significant), but can only be applied to a limited range of types.

Of course, consts are also limited to those values which are known at
compile-time.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 17 '05 #2
readonly can be set to a value in the constructor. Const cannot - its value
must be provided inline with the declaration.

"Axel Dahmen" <NO*****@NoOneKnows.invalid> wrote in message
news:eM**************@TK2MSFTNGP15.phx.gbl...
Hi,

can someone please enlighten me on the difference between a "const" and a
"readonly" declaration?

TIA,
Axel Dahmen

Nov 17 '05 #3

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...
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...
3
by: R.A.M. | last post by:
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/
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
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
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
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

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.