473,396 Members | 1,789 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.

const and readonly

What is the difference between two? when each one is used?

Thanks
Sep 29 '08 #1
7 1428
const is set at compile time, readonly is set within the constructor.

Sep 29 '08 #2
On Sep 29, 11:46*am, "Peter Morris" <mrpmorri...@SPAMgmail.comwrote:
const is set at compile time, readonly is set within the constructor.

Any reason to use readonly then?
Sep 29 '08 #3
On Mon, 29 Sep 2008 09:21:33 -0700, puzzlecracker <ir*********@gmail.com>
wrote:
On Sep 29, 11:46Â*am, "Peter Morris" <mrpmorri...@SPAMgmail.comwrote:
>const is set at compile time, readonly is set within the constructor.


Any reason to use readonly then?
Two reasons:

-- when you don't know the value of the variable at compile-time
-- when the variable is a type that doesn't support "const" (i.e. most
reference types)

Pete

Sep 29 '08 #4
Any reason to use readonly then?

Yes. When you have a class that accepts a value in its constructor, you
store the passed value but do not think it should change. Marking it
readonly makes sure you don't screw up later and try to change it.

Sep 29 '08 #5

Few more info

1) You cannot initialize a compile-time constant using the new operator
2) Const must be used when the value types are avaiable at compile time
3) For reference type constants use 'readonly'

"Peter Morris" <mr*********@SPAMgmail.comwrote in message
news:eg**************@TK2MSFTNGP03.phx.gbl...
>Any reason to use readonly then?

Yes. When you have a class that accepts a value in its constructor, you
store the passed value but do not think it should change. Marking it
readonly makes sure you don't screw up later and try to change it.

Sep 30 '08 #6
Newsgroup <ji*******@gmail.comwrote:
Few more info

1) You cannot initialize a compile-time constant using the new operator
2) Const must be used when the value types are avaiable at compile time
3) For reference type constants use 'readonly'
Strings are reference types, and they can be const - and in fact any
other reference type field can be const to, but the only supported
value is null :)

--
Jon Skeet - <sk***@pobox.com>
Web site: http://www.pobox.com/~skeet
Blog: http://www.msmvps.com/jon.skeet
C# in Depth: http://csharpindepth.com
Sep 30 '08 #7
On Sep 29, 8:21*pm, puzzlecracker <ironsel2...@gmail.comwrote:
On Sep 29, 11:46*am, "Peter Morris" <mrpmorri...@SPAMgmail.comwrote:
const is set at compile time, readonly is set within the constructor.

Any reason to use readonly then?
Versioning. When you make a library, you should only use non-private/
internal const if you can guarantee that the value won't change in the
future versions of the library. The reason is simple: when an
application references your library, and its code uses the constant,
the value of that constant is substituted directly into the compiled
assembly. Even if you later change the library to a newer one, the
compiled client code will still use the old value of the constant.

With readonly, it is not the case - it is a proper variable, and its
value is actually read at run-time when the assembly is loaded, so if
it changes in the assembly, the change will be seen by all clients on
the next run.

In general, when in doubt, prefer readonly. The only thing you really
lose is the ability to use it as a case label in a switch statement;
but you will certainly not introduce silent breakage, as is possible
with const and separate builds.
Oct 1 '08 #8

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

Similar topics

5
by: Kavitha | last post by:
Hi Pals, I am confused between const and static readonly in C#. I want to know the difference between then. Please give me clear explanation. thanks in advance. -- Kavitha
5
by: Vladimir Bezugliy | last post by:
Why I can't declare a member of class as "static const". As far as I see I should declare as "static readonly". class StateObject { public static const int BufferSize = 1024; } ...
17
by: My Name | last post by:
There was a topic on this earlier, but no answer, only people asking "Why do you want to do this..." Let's say I have a HUGE object and want to pass it to a routine that only needs read access...
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...
13
by: My4thPersonality | last post by:
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...
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
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
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...
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
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
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
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

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.