const 与 readonly知多少

Practical Difference between Const & ReadOnly

What is the difference between const and readonly?

Apart from the apparent difference of

  • having to declare the value at the time of a definition for a const   const  必须在定义的时候声明值

VS

readonly values can be computed dynamically but need to be assigned before the constructor exits.. after that it is frozen.  readonly可以动态计算,但是必须在构造函数退出前分配值,之后就被冻结

  • 'const's are implicitly static. You use a ClassName.ConstantName notation to access them.    const是隐式地static,所以你可以使用 类名.变量名 的符号来访问const变量

apart from除……之外

apparent表面的;显然的

implicitly隐式地

notation符号

There is a subtle difference. Consider a class defined in AssemblyA.   const和readonly之间还有一些细微的区别。考虑定义在AssemblyA中的类Const_V_Readonly

public class Const_V_Readonly
{
public const int I_CONST_VALUE = 2;
public readonly int I_RO_VALUE;
public Const_V_Readonly()
{
I_RO_VALUE = 3;
}
}

subtle  微妙的;精细的;敏感的;狡猾的;稀薄的

AssemblyB references AssemblyA and uses these values in code. When this is compiled,    AssemblyB引用了AssemblyA,并使用了const和readonly的值 ,代码编译之后

  • in the case of the const value, it is like a find-replace, the value 2 is 'baked into' the AssemblyB's IL. This means that if tomorrow I'll update I_CONST_VALUE to 20 in the future. AssemblyB would still have 2 till I recompile it.

const的值,像“搜索-替代”一样。const的值2直接被放到了AssemblyB的IL(·NET框架中中间语言)中。这意味着,如果明天我将const的值更新为20。AssemblyB中引用的const仍然为2,除非我重新编译AssemblyB

  • in the case of the readonly value, it is like a ref to a memory location. The value is not baked into AssemblyB's IL. This means that if the memory location is updated, AssemblyB gets the new value without recompilation. So if I_RO_VALUE is updated to 30, you only need to build AssemblyA. All clients do not need to be recompiled.

readonly,就像存储单元(内存)的一个引用。readonly变量的值,没有被直接编译到AssemblyB的IL中。这意味着,如果存储单元被更新了,那么AssemblyB将会获取新的值并且不用重新编译。所以,如果readonly的值被改变为30,你仅仅需要重新编译AssemblyA。而不是所有引用了AssemblyA的客户端需要重新编译。

So if you are confident that the value of the constant won't change use a const.   所以,如果你确信有一个常量不会改变,那么就使用const

public const int CM_IN_A_METER = 100;

But if you have a constant that may change (e.g. w.r.t. precision).. or when in doubt, use a readonly.    但是,如果你有一个常量可能会改变(比如 :精度) 或者有不确定是否会改变。那么使用readonly

public readonly float PI = 3.14;

Update: Aku needs to get a mention coz he pointed this out first. Also I need to plug where I learned this.. Effective C# - Bill Wagner

doubt  怀疑;疑问;疑惑

const string vs. static readonly string in c#

When you use a const string, the compiler embeds the string's value at compile-time.
Therefore, if you use a const value in a different
assembly, then update the original assembly and change the value, the
other assembly won't see the change until you re-compile it.

A static readonly string is a normal field that gets
looked up at runtime. Therefore, if the field's value is changed in a
different assembly, the changes will be seen as soon as the assembly is
loaded, without recompiling.

This also means that a static readonly string can use non-constant members, such as Environment.UserName or DateTime.Now.ToString(). A const string can only be initialized using other constants or literals.
Also, a static readonly string can be set in a static constructor; a const string can only be initialized inline.

Note that a static string can be modified; you should use static readonly instead.

 
  +1, And the obvious conclusion, from a practical and semantic point-of-view: const should only be used for constants - constants being values that never, ever, ever change.

const和readonly的区别的更多相关文章

  1. C#中 const 和 readonly 的区别

    C#中 const 和 readonly 的区别 来源 https://www.cnblogs.com/gsk99/archive/2008/10/10/1308299.html http://dev ...

  2. C# 关键字const与readonly的区别

    尽管你写了很多年的C#的代码,但是可能当别人问到你const与readonly的区别时候,还是会小小的愣一会吧~ 笔者也是在看欧立奇版的<.Net 程序员面试宝典>的时候,才发现自己长久以 ...

  3. const与readonly的区别

    const与readonly 很像,都是将变量声明为只读,且在变量初始化后就不可改写.那么,const与readonly 这两个修饰符到底区别在什么地方呢?其实,这个牵扯出C#语言中两种不同的常量类型 ...

  4. const 与 readonly的区别

    首先先解释下什么是静态常量以及什么是动态常量. 静态常量是指编译器在编译时候会对常量进行解析,并将常量的值替换成初始化的那个值. 动态常量的值则是在运行的那一刻才获得的,编译器编译期间将其标示为只读常 ...

  5. C#中Const和Readonly的区别

    const 的概念就是一个包含不能修改的值的变量.常数表达式是在编译时可被完全计算的表达式.因此不能从一个变量中提取的值来初始化常量.如果 const int a = b+1;b是一个变量,显然不能再 ...

  6. const 与 readonly知多少

    原文地址: http://www.cnblogs.com/royenhome/archive/2010/05/22/1741592.html 尽管你写了很多年的C#的代码,但是可能当别人问到你cons ...

  7. [转]const 与 readonly知多少

    引自:http://www.cnblogs.com/royenhome/archive/2010/05/22/1741592.html 尽管你写了很多年的C#的代码,但是可能当别人问到你const与r ...

  8. const和readonly你真的懂吗?

    第二遍文章我打算把const和readonly的区别拿出来讲下,因为写代码这么久我都还没搞清楚这两者的区别,实在有点惭愧,所以这一次我打算搞清楚它. 定义 来看看MSDN的解释: readonly:r ...

  9. const和readonly区别

    内容来源<<你必须知道的.NET>>(转载) 标题:什么才是不变:const和readonly 内容: const:用 const 修饰符声明的成员叫常量,是在编译期初始化并嵌 ...

随机推荐

  1. iOS JSON解析

    解析json成dic对象 -(void)fetchedData:(NSData*)responseData {//parse out the json dataNSError* error; NSDi ...

  2. iOS 进阶 第十二天(0413)

    0413 - Quartz2D 使用Quartz2D画图形步骤:(drawRect: 方法 是 在view第一次显示到屏幕上的时候会调用一次.是系统主动去掉用的,不能直接调用.那么问题来了,比如我要通 ...

  3. Java对象的序列化与反序列化

    序列化与反序列化 序列化 (Serialization)是将对象的状态信息转换为可以存储或传输的形式的过程.一般将一个对象存储至一个储存媒介,例如档案或是记亿体缓冲等.在网络传输过程中,可以是字节或是 ...

  4. 初解DLL基本知识

    1.DLL基本理论 在Windows操作系统中,几乎所有的内容都是以DLL的形式存在的. 1.DLL基本概念 语言程序要从目标代码(.obj)外部引用函数,可以通过俩种途径实现——静态链接和动态链接. ...

  5. 服务器端spice配置详解

    1. 安装必要的工具 sudo apt-get install build-essential autoconf git-core intltool 2. 安装必要的依赖包 -dev libxfixe ...

  6. c语言关于二进制的输出

    c语言中的二进制输出是没有占位符的,不像八进制:%o: 和十六进制:x%: c中二进制的输出 //右移31位,从最高为开始和1做&运算,得到每一位的二进制数值 void printbinry( ...

  7. HTTP 错误 404.3 - Not Found由于扩展配置问题而无法提供您请求的页面。如果该页面是脚本,请添加处理程序。如果应下载文件,请添加 MIME 映射

    出现此错误可能是在安装IIS服务器的时候没有勾选“应用程序开发”, 解决方法如下: 就像安装IIS一样的步骤. 选择web服务器(IIS)后,再选择“应用程序开发”  选择asp.net  以及 服务 ...

  8. python调用C语言

    标签(空格分隔): python test.c代码如下 #include<stdio.h> void display(char* msg) { printf("%s\n" ...

  9. Feature Extraction

    http://www.erogol.com/ml-work-flow-part-3-feature-extraction/

  10. JavaScript高级---装饰者模式设计

    一.设计模式 javascript里面给我们提供了很多种设计模式: 工厂.桥.组合.门面.适配器.装饰者.享元.代理.观察者.命令.责任链 在前面我们实现了工厂模式和桥模式 工厂模式 : 核心:为了生 ...