
{"id":1186,"date":"2013-01-11T09:59:36","date_gmt":"2013-01-11T14:59:36","guid":{"rendered":"http:\/\/www.ikriv.com\/blog\/?p=1186"},"modified":"2013-01-11T09:59:36","modified_gmt":"2013-01-11T14:59:36","slug":"net-string-constants-vs-static-readonly-fields","status":"publish","type":"post","link":"https:\/\/ikriv.com\/blog\/?p=1186","title":{"rendered":".NET: String Constants vs Static Readonly Fields"},"content":{"rendered":"<p>Numerous books and articles explain the difference between<\/p>\n<p><code>public const string Foo = \"foo\";<\/code> and<br \/>\n<code>public static readonly string Foo = \"foo\";<\/code><\/p>\n<p>The former is treated as true constant that never ever changes, and it may be baked verbatim into the code of any caller:<\/p>\n<p><code>const string CarthageFate = \"Carthago delenda est\";<\/code><\/p>\n<p>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<\/p>\n<p><code>public static readonly string InitTime = DateTime.Now.ToString();<\/code><\/p>\n<p>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 you enjoyment: <a href=\"http:\/\/www.ikriv.com\/dev\/dotnet\/StringConstant.zip\">StringConstant.zip<\/a>.<\/p>\n<p>We have two versions of a <code>DefiningLib<\/code> library defining some constants and readonly fields, and a <code>UsingApp<\/code> that uses it.<\/p>\n<p><code><\/p>\n<pre>public const string VersionConst = \"v1\";\npublic static readonly string VersionField = \"v1\";\npublic static readonly string InitTime = DateTime.Now.ToString();<\/pre>\n<p><\/code><\/p>\n<p>Version 1 is compiled by the standard &#8220;Debug&#8221; configuration and produces the following output:<\/p>\n<p><code><\/p>\n<pre>DefiningLib version: 1.0.0.0\nInit time: 1\/11\/2013 9:51:38 AM\nVersionConst: v1\nVersionfield: v1<\/pre>\n<p><\/code><\/p>\n<p>Then we compile version 2 of the defining lib by switching to &#8220;Debug.v2&#8221; solution configuration. Version 2 looks like this:<\/p>\n<p><code><\/p>\n<pre>public const string VersionConst = \"v2\";\npublic static readonly string VersionField = \"v2\";\npublic static readonly string InitTime = DateTime.Now.ToString();<\/pre>\n<p><\/code><\/p>\n<p>Only <code>DefiningLib<\/code> changes, <code>UsingApp<\/code> stays the same. We then manually copy <code>DefiningLib.dll<\/code> from <code>DefiningLib\\bin\\Debug.v2<\/code> folder to <code>UsingApp\\bin\\Debug<\/code> and invoke <code>UsingApp.exe<\/code>. The output is as follows:<\/p>\n<p><code><\/p>\n<pre>DefiningLib version: 2.0.0.0\nInit time: 1\/11\/2013 9:54:06 AM\nVersionConst: v1\nVersionfield: v2<\/pre>\n<p><\/code><\/p>\n<p>Voila, the theory is indeed right. The constant was baked in into <code>UsingApp.exe<\/code> and stayed &#8220;v1&#8221;. The field reference was updated to &#8220;v2&#8221; as expected.<\/p>\n<p>Lesson learned: if there is even a remote possibility that your &#8220;constant&#8221; value might change in the next version, make it a readonly field.<a style=\"display:none\" href=\"http:\/\/www.codeproject.com\/script\/Articles\/BlogFeedList.aspx?amid=1181663\" rel=\"tag\">CodeProject<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Numerous books and articles explain the difference between public const string Foo = &#8220;foo&#8221;; and public static readonly string Foo = &#8220;foo&#8221;; The former is treated as true constant that <a href=\"https:\/\/ikriv.com\/blog\/?p=1186\" class=\"more-link\">[&hellip;]<\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"Layout":"","footnotes":""},"categories":[3,8,16,5],"tags":[],"class_list":["entry","author-ikriv","post-1186","post","type-post","status-publish","format-standard","category-dotnet","category-cs","category-demos","category-dev"],"_links":{"self":[{"href":"https:\/\/ikriv.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/1186","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/ikriv.com\/blog\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/ikriv.com\/blog\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/ikriv.com\/blog\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/ikriv.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=1186"}],"version-history":[{"count":0,"href":"https:\/\/ikriv.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/1186\/revisions"}],"wp:attachment":[{"href":"https:\/\/ikriv.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=1186"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/ikriv.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=1186"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/ikriv.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=1186"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}