Click here to Skip to main content
15,879,326 members
Articles / Programming Languages / C#

One reason to prefer readonly to const in C#

Rate me:
Please Sign up or sign in to vote.
4.87/5 (37 votes)
21 Apr 2010CPOL2 min read 44.1K   17   11
One reason to prefer readonly to const in C#

First off, let’s talk about what’s going on in the C# compiler when you use const or readonly in your field definitions. The const qualifier can be used with primitive data types, and strings only. When used, the value assigned to a const field is inserted directly in all its references in the generated IL code. This is true about other assemblies too. Other assemblies that refer to that const field are compiled as if they have used directly the value itself. This can be the source of problem that I’m going to talk about soon. Readonly fields are run-time constants. They occupy some place in memory, and references to them are resolved in run-time, as if we have referred to an ordinary variable. Actually they are variables that resemble constants.

Imagine that you have created and released a project to the public. Your project contains several assemblies in the form of .dll files. They make use of some constant value in one of the .dll files, e.g., SomeLibrary.dll stores a constant value in one of its classes, e.g.,

C# – using const

1
2
3
4
5
6
C#
public class Options
{
   ...
   public const int NetworkTimeout = 2000; 
   ...
}

You realize that the value assigned to NetworkTimeout is less than expected, so you decide to update SomeLibrary.dll files in all your customer machines with a new one in which NetworkTimeout is set to 3000. But it will not work. Because all references to NetworkTimeout in other assemblies have been replaced with the constant 2000, and the new value will not be fetched any more. In this case, the problem will be solved only when all other assemblies are rebuilt. No other update scenarios will do.

But if we have used readonly instead of const, the problem would have been solved with updating SomeLibrary.dll only.

C# – using readonly

1
2
3
4
5
6
C#
public class Options
{
    ...
    public static readonly int NetworkTimeout = 2000;
    ...
}

The static modifier has been added only to make the two codes above compatible. Note that all const fields are also static, but readonly fields can be either static or an instance field.


Filed under: C#, Programming, Tips and Tricks Tagged: C#, readonly const

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Software Developer
Australia Australia
A software designer and developer

Comments and Discussions

 
GeneralMy vote of 5 Pin
Joezer BH23-Aug-15 1:36
professionalJoezer BH23-Aug-15 1:36 
GeneralMy vote of 5 Pin
Chamila Nishantha12-Sep-13 17:21
Chamila Nishantha12-Sep-13 17:21 
GeneralMy vote of 5 Pin
Leonardo Paneque30-Apr-12 11:51
Leonardo Paneque30-Apr-12 11:51 
GeneralOther problems... Pin
Richard Deeming27-Apr-10 5:35
mveRichard Deeming27-Apr-10 5:35 
AnswerDisadvantages PinPopular
Eugene Sichkar26-Apr-10 0:38
Eugene Sichkar26-Apr-10 0:38 
QuestionA "reason to prefer" or an "important distinction"? Pin
supercat922-Apr-10 5:30
supercat922-Apr-10 5:30 
AnswerRe: A "reason to prefer" or an "important distinction"? Pin
Sina Iravanian22-Apr-10 20:55
Sina Iravanian22-Apr-10 20:55 
AnswerRe: A "reason to prefer" or an "important distinction"? Pin
ronlease27-Apr-10 4:02
professionalronlease27-Apr-10 4:02 
"What might be helpful would be a means of requesting that a field be considered "publicly" ReadOnly" but "Privately" writable. Not sure how to do that, though."

public string Name{get;private set;}


Since having public fields is generally a bad OO practice, the above property will give you the functionality you want.
GeneralRe: A "reason to prefer" or an "important distinction"? Pin
Sina Iravanian27-Apr-10 4:56
Sina Iravanian27-Apr-10 4:56 
GeneralWell written... Pin
Sandeep Mewara21-Apr-10 19:15
mveSandeep Mewara21-Apr-10 19:15 
GeneralRe: Well written... Pin
Sina Iravanian22-Apr-10 0:25
Sina Iravanian22-Apr-10 0:25 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.