<?xml version="1.0" encoding="UTF-8"?> <rss version="2.0"
xmlns:content="http://purl.org/rss/1.0/modules/content/"
xmlns:wfw="http://wellformedweb.org/CommentAPI/"
xmlns:dc="http://purl.org/dc/elements/1.1/"
xmlns:atom="http://www.w3.org/2005/Atom"
xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
><channel><title>简单生活 —— Kevin Yang的博客 &#187; 只读</title> <atom:link href="http://www.imkevinyang.com/tags/%e5%8f%aa%e8%af%bb/feed" rel="self" type="application/rss+xml" /><link>http://www.imkevinyang.com</link> <description>It&#039;s all about sharing</description> <lastBuildDate>Mon, 06 Sep 2010 08:00:00 +0000</lastBuildDate> <generator>http://wordpress.org/?v=2.9.1</generator> <language>en</language> <sy:updatePeriod>hourly</sy:updatePeriod> <sy:updateFrequency>1</sy:updateFrequency> <item><title>C#中Readonly和const的区别</title><link>http://www.imkevinyang.com/2009/11/c%e4%b8%adreadonly%e5%92%8cconst%e7%9a%84%e5%8c%ba%e5%88%ab.html</link> <comments>http://www.imkevinyang.com/2009/11/c%e4%b8%adreadonly%e5%92%8cconst%e7%9a%84%e5%8c%ba%e5%88%ab.html#comments</comments> <pubDate>Thu, 12 Nov 2009 06:05:00 +0000</pubDate> <dc:creator>Kevin Yang</dc:creator> <category><![CDATA[其他随笔]]></category> <category><![CDATA[Const]]></category> <category><![CDATA[CSharp]]></category> <category><![CDATA[Readonly]]></category> <category><![CDATA[只读]]></category> <category><![CDATA[常量定义]]></category><guid isPermaLink="false">http://www.imkevinyang.com/2009/11/c%e4%b8%adreadonly%e5%92%8cconst%e7%9a%84%e5%8c%ba%e5%88%ab.html</guid> <description><![CDATA[<p>这个问题其实很常识。写代码的时候突然想到的，作此备忘。</p><p>const和readonly这两个关键字都是用来表示常量，只不过标记为Readonly的变量可以在构造函数中动态赋值，const需要在变量声明的时候就初始化；如果将这两种常量放到单独的动态链接库中，然后编译整个项目，则应用程序对于readonly变量，是每次都从动态链接库中读取最新的常量值，而对于const变量是在编译的时候就写死到应用程序代码中了（我不知道这是不是编译器优化行为，反&#8230;</p>]]></description> <content:encoded><![CDATA[<p>这个问题其实很常识。写代码的时候突然想到的，作此备忘。</p><p>const和readonly这两个关键字都是用来表示常量，只不过标记为Readonly的变量可以在构造函数中动态赋值，const需要在变量声明的时候就初始化；如果将这两种常量放到单独的动态链接库中，然后编译整个项目，则应用程序对于readonly变量，是每次都从动态链接库中读取最新的常量值，而对于const变量是在编译的时候就写死到应用程序代码中了（我不知道这是不是编译器优化行为，反正我在VS2008下无论是Debug版本还是Release版本均是这样的行为）。</p><p>后面一点很重要，举一个简单的例子。我新建了两个工程，一个应用程序，一个是类库。</p><p><img style="border-right-width: 0px; display: inline; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px" title="image" border="0" alt="image" src="http://www.imkevinyang.com/wp-content/uploads/2009/11/image_thumb9.png" width="168" height="160" /></p><p>其中ClassLibrary1中定义了StaticResources类</p><pre class="csharpcode"><span class="kwrd">public</span> <span class="kwrd">class</span> StaticResources
{
    <span class="kwrd">public</span> <span class="kwrd">const</span> <span class="kwrd">int</span> ConstVar = 1000;
    <span class="kwrd">public</span> <span class="kwrd">static</span> <span class="kwrd">readonly</span> <span class="kwrd">int</span> ReadonlyVar = 1000;
}</pre><p>在ConsoleApplication5中代码如下：</p><pre class="csharpcode"><span class="kwrd">class</span> Program
{
    <span class="kwrd">static</span> <span class="kwrd">void</span> Main(<span class="kwrd">string</span>[] args)
    {
        Console.WriteLine(<span class="str">&quot;Const Variable is {0}&quot;</span>, StaticResources.ConstVar);
        Console.WriteLine(<span class="str">&quot;Readonly Variable is {0}&quot;</span>, StaticResources.ReadonlyVar);
    }
}</pre><p>输出都是1000没有问题。</p><p>当写类库的人突然因为种种原因需要更新ConstVar和ReadonlyVar这两个常量，假设更新为2000。更新完之后，他觉得主应用程序没有做修改，就没有更新，只发布了最新的dll。结果呢，ReadonlyVar更新了，ConstVar没有更新。</p><p><img style="border-right-width: 0px; display: inline; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px" title="image" border="0" alt="image" src="http://www.imkevinyang.com/wp-content/uploads/2009/11/image_thumb10.png" width="225" height="42" /></p><p>对于Asp.net网站应用程序就更容易出现这样的问题了。一般我们引用的动态链接库都是放在网站下的bin目录。因为更新网站相对比较麻烦，所以有时候我们会偷懒，只更新动态链接库，这个时候就很容易出现上述的问题了。</p><p>鉴于此，一般我在定义常量的时候比较偏向于使用static readonly组合关键字。虽然有稍许的性能缺失，但是带来的更好的可维护性。</p><p align="right">——<a href="http://www.imkevinyang.com/"><em><strong>Kevin Yang</strong></em></a></p>标签：<a href="http://www.imkevinyang.com/tags/const" title="Const" rel="tag">Const</a>, <a href="http://www.imkevinyang.com/tags/csharp" title="CSharp" rel="tag">CSharp</a>, <a href="http://www.imkevinyang.com/tags/readonly" title="Readonly" rel="tag">Readonly</a>, <a href="http://www.imkevinyang.com/categories/techarticles/othertecharticles" title="其他随笔" rel="tag">其他随笔</a>, <a href="http://www.imkevinyang.com/tags/%e5%8f%aa%e8%af%bb" title="只读" rel="tag">只读</a>, <a href="http://www.imkevinyang.com/tags/%e5%b8%b8%e9%87%8f%e5%ae%9a%e4%b9%89" title="常量定义" rel="tag">常量定义</a><br /><h4 style="background-color:#3B3B3B;border-bottom:2px groove gray;color:#F2F2F2;margin-top:20px;padding:6px 6px 6px 15px;margin:20px 0px 0px 0px">你可能对下面的文章感兴趣</h4><ul class="st-related-posts"><li><a href="http://www.imkevinyang.com/2009/11/%e6%85%8e%e7%94%a8%e7%b1%bb%e5%9e%8b%e5%bc%ba%e5%88%b6%e8%bd%ac%e6%8d%a2.html" title="慎用类型强制转换 (2009/11/12)">慎用类型强制转换</a> (2009/11/12)</li><li><a href="http://www.imkevinyang.com/2009/03/%e9%9a%8f%e6%9c%ba%e5%ad%97%e7%ac%a6%e4%b8%b2%e7%94%9f%e6%88%90%e5%87%bd%e6%95%b0.html" title="随机字符串生成函数 (2009/03/23)">随机字符串生成函数</a> (2009/03/23)</li></ul>]]></content:encoded> <wfw:commentRss>http://www.imkevinyang.com/2009/11/c%e4%b8%adreadonly%e5%92%8cconst%e7%9a%84%e5%8c%ba%e5%88%ab.html/feed</wfw:commentRss> <slash:comments>0</slash:comments> </item> </channel> </rss>