Click here to Skip to main content
16,020,628 members
Articles / Programming Languages / C#

.NET: String Constants vs Static Readonly Fields

Rate me:
Please Sign up or sign in to vote.
4.29/5 (7 votes)
13 Jan 2013Apache1 min read 27.9K   6   5
If there is even a remote possibility that your “constant” value might change in the next version, make it a readonly field.

Numerous books and articles explain the difference between:

C#
public const string Foo = "foo";

and:

C#
public static readonly string Foo = "foo";

The former is treated as a true constant that never ever changes, and it may be baked verbatim into the code of any caller:

C#
const string CarthageFate = "Carthago delenda est";

The latter is treated as a field that might actually change between assembly versions, program invocations, or even in different app domains. You can actually do things like:

C#
public static readonly string InitTime = DateTime.Now.ToString();

So, I read about all that, but I never tested it. Until now, that is. Since this fact was material for my current project, I wrote a little test that I offer for your enjoyment: StringConstant.zip.

We have two versions of a DefiningLib library defining some constants and readonly fields, and a UsingApp that uses it.

C#
public const string VersionConst = "v1";
public static readonly string VersionField = "v1";
public static readonly string InitTime = DateTime.Now.ToString();

Version 1 is compiled by the standard “Debug” configuration and produces the following output:

DefiningLib version: 1.0.0.0
Init time: 1/11/2013 9:51:38 AM
VersionConst: v1
Versionfield: v1

Then, we compile version 2 of the defining lib by switching to “Debug.v2″ solution configuration. Version 2 looks like this:

C#
public const string VersionConst = "v2";
public static readonly string VersionField = "v2";
public static readonly string InitTime = DateTime.Now.ToString();

Only DefiningLib changes, UsingApp stays the same. We then manually copy DefiningLib.dll from DefiningLib\bin\Debug.v2 folder to UsingApp\bin\Debug and invoke UsingApp.exe. The output is as follows:

DefiningLib version: 2.0.0.0
Init time: 1/11/2013 9:54:06 AM
VersionConst: v1
Versionfield: v2

Voila, the theory is indeed right. The constant was baked in into UsingApp.exe and stayed “v1″. The field reference was updated to “v2″ as expected.

Lesson learned: If there is even a remote possibility that your “constant” value might change in the next version, make it a readonly field.

This article was originally posted at http://www.ikriv.com/blog?p=1186

License

This article, along with any associated source code and files, is licensed under The Apache License, Version 2.0


Written By
Technical Lead Thomson Reuters
United States United States
Ivan is a hands-on software architect/technical lead working for Thomson Reuters in the New York City area. At present I am mostly building complex multi-threaded WPF application for the financial sector, but I am also interested in cloud computing, web development, mobile development, etc.

Please visit my web site: www.ikriv.com.

Comments and Discussions

 
NewsSystem.String.Empty Pin
Andrei Ion Rînea14-Jan-13 13:38
Andrei Ion Rînea14-Jan-13 13:38 
Questionreadonly is not really read-only Pin
tvbusy13-Jan-13 22:41
tvbusy13-Jan-13 22:41 
AnswerRe: readonly is not really read-only Pin
Paulo Morgado14-Jan-13 9:24
professionalPaulo Morgado14-Jan-13 9:24 
GeneralMy vote of 3 Pin
arbinda_dhaka13-Jan-13 9:05
arbinda_dhaka13-Jan-13 9:05 
GeneralRe: My vote of 3 Pin
Ivan Krivyakov13-Jan-13 9:12
Ivan Krivyakov13-Jan-13 9:12 

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.