473,654 Members | 3,028 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Difference between const and readonly

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_DOCUME NT"

From what I know about const, every call to CurDoc will be replaced with the
text "CURRENT_DOCUME NT" during compilation. Is this right? If yes then
wouldn't it be better to declare it as:
public shared readonly CurDoc as string = "CURRENT_DOCUME NT"

This way there'll be a reference to this string, instead of it being copied
at each access point. What's the best practice for these?
Thanks
Amit
Nov 21 '05 #1
2 3548
From what I know about const, every call to CurDoc will be replaced with the
text "CURRENT_DOCUME NT" during compilation. Is this right?
Sort of. There's no "calling" going on though.

This way there'll be a reference to this string, instead of it being copied
at each access point. What's the best practice for these?


That's what you'll get when using a const too, without the need for a
field access.

Mattias

--
Mattias Sjögren [MVP] mattias @ mvps.org
http://www.msjogren.net/dotnet/ | http://www.dotnetinterop.com
Please reply only to the newsgroup.
Nov 21 '05 #2
Amit,
In addition to the other comments:

The important distinction between Const & Readonly is when the field is
defined in another assembly.

For example:

' Project 1
public Const ConstDoc as string = "CURRENT_DOCUME NT"
public Shared Readonly ReadOnlyDoc as string = "CURRENT_DOCUME NT"
' Project 2
' references Project 1

When you use ConstDoc in Project 2, a copy of the literal is placed in
Project 2 assembly, if you recompile Project 1 without recompiling Project
2. Project 2 will continue to use the original value of ConstDoc.

Where as when you use ReadOnlyDoc in Project 2, Project 2 will ask Project 1
for the value at runtime, allowing you to freely recompile Project 1 without
recompiling Project 2.

Also Const will be evaluated (known) at compile time, where as Readonly can
be computed at run time.

Another important difference is that Const are implicitly Shared, where as
Readonly can be either a Shared or instance field. For example
EventArgs.Empty is an example of a Shared Readonly field, I will use Shared
instance fields for fields that are not known until I construct the object,
however I want to ensure the value of the field never changes...

Hope this helps
Jay

"Amit" <re***********@ hotmail.com> wrote in message
news:%2******** ********@TK2MSF TNGP14.phx.gbl. .. 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_DOCUME NT"

From what I know about const, every call to CurDoc will be replaced with
the
text "CURRENT_DOCUME NT" during compilation. Is this right? If yes then
wouldn't it be better to declare it as:
public shared readonly CurDoc as string = "CURRENT_DOCUME NT"

This way there'll be a reference to this string, instead of it being
copied
at each access point. What's the best practice for these?
Thanks
Amit

Nov 21 '05 #3

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

Similar topics

5
343
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
44714
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; } F:\.NET\ChatterBox\MessageListener.cs(15): The constant
17
8865
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 to it. It would sure be nice to declare the receiving routine to be something like: void DoSomething(const ref HugeThing) { ... }
19
7708
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 from binary incompatibility since it is hardbaked into the binary. I do not believe there is a performance advantage with using const over static readonly since the JIT will take account of that or am I wrong here?
13
6008
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 refers to: const long size = ((long)int.MaxValue + 1) / 4; Constant-Value must be computable at compile time readonly DateTime date;
3
1497
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
2997
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 occurences. i have been looking at both static and const variables as the options, but couldn't decide on one.
2
10090
by: wizofaus | last post by:
Given the following code: public class Test { static unsafe void StringManip(string data) { fixed (char* ps = data) ps = '$'; }
7
1435
by: puzzlecracker | last post by:
What is the difference between two? when each one is used? Thanks
0
8375
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8290
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8707
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
8482
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
7306
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6161
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 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 a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5622
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4294
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2714
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system

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.