1. const和readonly的值一旦初始化则都不再可以改写;
  2. const只能在声明时初始化;readonly既可以在声明时初始化也可以在构造器中初始化;
  3. const隐含static,不可以再写static const;readonly则不默认static,如需要可以写static readonly;
  4. const是编译期静态解析的常量(因此其表达式必须在编译时就可以求值);readonly则是运行期动态解析的常量;
  5. const既可用来修饰类中的成员,也可修饰函数体内的局部变量;readonly只可以用于修饰类中的成员

前面是我从网上摘录的,文字太多,懒得自己再总结和打字了.

注意,第四点尤为重要,我用以下代码来说明和验证:

1.建立文本文件,然后改成.cs文件-lib.cs:

  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. namespace ConstReadonly_Base
  6. {
  7. public class Test
  8. {
  9. public const double PI = 3.14;
  10. public static readonly double pi = 3.14;
  11. }
  12. }

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ConstReadonly_Base
{
public class Test
{
public const double PI = 3.14;
public static readonly double pi = 3.14;
}
}

2.在命令行中输入: csc /t:library lib.cs,就会生成一个lib.dll文件.
3.建立文本文件,然后改成.cs文件-demo.cs:

  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. namespace ConstReadonly_Demo
  6. {
  7. class Program
  8. {
  9. static void Main(string[] args)
  10. {
  11. Console.WriteLine(ConstReadonly_Base.Test.PI);
  12. Console.WriteLine(ConstReadonly_Base.Test.pi);
  13. Console.ReadKey();
  14. }
  15. }
  16. }

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ConstReadonly_Demo
{
class Program
{
static void Main(string[] args)
{
Console.WriteLine(ConstReadonly_Base.Test.PI);
Console.WriteLine(ConstReadonly_Base.Test.pi);
Console.ReadKey();
}
}
}

4.在命令行中输入: csc /r:lib.dll demo.cs,也就会生成一个demo.exe文件.
双击,结果如下:
3.14
3.14
5.更改lib.cs为:
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. namespace ConstReadonly_Base
  6. {
  7. public class Test
  8. {
  9. public const double PI = 3.1415;
  10. public static readonly double pi = 3.1415;
  11. }
  12. }

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ConstReadonly_Base
{
public class Test
{
public const double PI = 3.1415;
public static readonly double pi = 3.1415;
}
}

6.再次双击demo.exe文件,结果却如下:
3.14
3.1415
(注: 直接双击demo.exe是直接运行,不再编译,上结果也就是运行期结束的结果,相当于跳过了编译)
 
原因是什么呢?请听我慢慢道来.
用ILDasm工具(这里假定你会用),选择lib.dll,内容如下:
双击PI段:
内容为
  1. .field public static literal float64 PI = float64(3.1415000000000002)

.field public static literal float64 PI = float64(3.1415000000000002)

双击pi段:
内容为
  1. .field public static initonly float64 pi

.field public static initonly float64 pi

明显lib.cs被编译成元数据和IL汇编时,PI直接被替换成float64(3.1415000000000002)

而.cctor: void()静态构造函数中,

  1. .method private hidebysig specialname rtspecialname static
  2. void  .cctor() cil managed
  3. {
  4. // Code size       15 (0xf)
  5. .maxstack  8
  6. IL_0000:  ldc.r8     3.1415000000000002
  7. IL_0009:  stsfld     float64 ConstReadonly_Base.Test::pi
  8. IL_000e:  ret
  9. } // end of method Test::.cctor

.method private hidebysig specialname rtspecialname static
void .cctor() cil managed
{
// Code size 15 (0xf)
.maxstack 8
IL_0000: ldc.r8 3.1415000000000002
IL_0009: stsfld float64 ConstReadonly_Base.Test::pi
IL_000e: ret
} // end of method Test::.cctor

pi是动态分配内存的,直到运行时才调用静态构造函数来初始化pi.

如果这时你还是不太明白,让我们接下去看:

用ILDasm工具,选择demo.exe,内容如下:

我们来看看Main函数中我们到底做了些什么:

  1. .method private hidebysig static void  Main(string[] args) cil managed
  2. {
  3. .entrypoint
  4. // Code size       34 (0x22)
  5. .maxstack  8
  6. IL_0000:  nop
  7. IL_0001:  ldc.r8     3.1400000000000001
  8. IL_000a:  call       void [mscorlib]System.Console::WriteLine(float64)
  9. IL_000f:  nop
  10. IL_0010:  ldsfld     float64 ['ConstReadonly-Base']ConstReadonly_Base.Test::pi
  11. IL_0015:  call       void [mscorlib]System.Console::WriteLine(float64)
  12. IL_001a:  nop
  13. IL_001b:  call       valuetype [mscorlib]System.ConsoleKeyInfo [mscorlib]System.Console::ReadKey()
  14. IL_0020:  pop
  15. IL_0021:  ret
  16. } // end of method Program::Main

.method private hidebysig static void Main(string[] args) cil managed
{
.entrypoint
// Code size 34 (0x22)
.maxstack 8
IL_0000: nop
IL_0001: ldc.r8 3.1400000000000001
IL_000a: call void [mscorlib]System.Console::WriteLine(float64)
IL_000f: nop
IL_0010: ldsfld float64 ['ConstReadonly-Base']ConstReadonly_Base.Test::pi
IL_0015: call void [mscorlib]System.Console::WriteLine(float64)
IL_001a: nop
IL_001b: call valuetype [mscorlib]System.ConsoleKeyInfo [mscorlib]System.Console::ReadKey()
IL_0020: pop
IL_0021: ret
} // end of method Program::Main

WriteLine()打印PI时,

  1. IL_0001:  ldc.r8     3.1400000000000001
  2. IL_000a:  call       void [mscorlib]System.Console::WriteLine(float64)

IL_0001: ldc.r8 3.1400000000000001
IL_000a: call void [mscorlib]System.Console::WriteLine(float64)

这说明编译时,PI被编译成一个常量.

WriteLine()打印pi时,

  1. IL_0010:  ldsfld     float64 ['ConstReadonly-Base']ConstReadonly_Base.Test::pi

IL_0010: ldsfld float64 ['ConstReadonly-Base']ConstReadonly_Base.Test::pi

这说明编译时,pi被编译成调用ConstReadonly_Base.Test::pi,可见pi量是动态分配和调用的.

总结一下const和readonly的更多相关文章

  1. C#基础知识七之const和readonly关键字

    前言 不知道大家对const和readonly关键字两者的区别了解多少,如果你也不是很清楚的话,那就一起来探讨吧!探讨之前我们先来了解静态常量和动态常量. 静态常量 所谓静态常量就是在编译期间会对变量 ...

  2. const 与 readonly知多少

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

  3. [c#基础]关于const和readonly常见的笔试题剖析

    引言 有那么几天没更新博客了,发现到了不得不写的地步,总是有那么个声音在强迫自己,虽然工作很累,但是有些东西不写出来,不能原谅自己.今天为什么总结这两个关键字的区别,总觉得这两个关键字的用法用的太习惯 ...

  4. const 和 readonly

    const 和 readonly 的异同 Const readonly 字面意 不变常量,不可修改 只读操作,不可写 初始化 必须在声明的同时赋值 可在声明和构造方法中进行赋值 所属关系 类.即sta ...

  5. C#夯实基础系列之const与readonly

    一.const与readonly的争议       你一定写过const,也一定用过readonly,但说起两者的区别,并说出何时用const,何时用readonly,你是否能清晰有条理地说出个一二三 ...

  6. 读书笔记:const和readonly、static readonly 那些事

    C#中表示不变的量(常量)的两种形式:const 和readonly const 是静态常量 readonly 是动态常量 严格的来讲:const 应该称为常量 而readonly 则应称为只读变量. ...

  7. const和readonly区别

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

  8. [转]const 与 readonly知多少

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

  9. const与readonly深度分析(.NET)

    前言 很多.NET的初学者对const和readonly的使用很模糊,本文就const和readonly做一下深度分析,包括: 1. const数据类型的优势 2. const数据类型的劣势 3. r ...

  10. [c#] const 与 readonly

    c# 中 const 与 readonly 关键字看似相同,实则不同.重点在于确定值的时间. const const 很简单,就是一个常量,不可以被 static 修饰,因为被 const 修饰的字段 ...

随机推荐

  1. haporoxy的keeplaive ZZ

    转载一个别人总结的http://hi.baidu.com/%D3%E3%B5%C1%B5%C1/blog/item/04ffd1b1854f69ef30add1e8.html httpclose, h ...

  2. Kettle汇总时参数

    Kettle汇总时手动执行小时汇总命令: ./kitchen.sh -norep -file /usr/local/evqm/kettle/kettle_scripts/rpt_hour.kjb -p ...

  3. mac 卸载 XCode

    http://blog.csdn.net/songques/article/details/7244144

  4. 关于 Lua 内存泄漏的检测

    前一阵开始和同事一起优化内存,首先是优化 Lua 内存,因为发现每次战斗完后 Lua 内存非常大,从 3M 左右在经过了10次左右的战斗后,会暴增到近 100M,很明显是有内存泄漏.      然后我 ...

  5. The Moving Points

    Problem Description There are N points in total. Every point moves in certain direction and certain ...

  6. 利用fgets,fputs的回显程序

    #include <stdio.h> #define MAXLINE 20 int main(void) { char line[MAXLINE]; while(fgets(line,MA ...

  7. android智能家居在线语音控制

    对于android 智能家居项目,如果能实现语音控制,无疑会丰富项目功能,改善用户体验,android语音识别的方法有三种:一是使用intent调用语音识别程序,二 是应用程序自己调用语音识别库,三是 ...

  8. Oracle- 用户管理

    Oracle一个数据库里可以分配多个用户,用户创建自己的表,自己创建的表如果不想分配给其他用户使用,其他用户是看不到自己的创建的表的. 用户管理: 创建用户: create user chunxiao ...

  9. WinForm简单多国语言实现

    参考:http://minmin86121.blog.163.com/blog/static/4968115720119259151898/ http://www.cnblogs.com/hakuci ...

  10. QT下自定义QQ聊天窗口tab控件

    1.用继承pushbutton派生类来实现tab按钮,里面加一个QPushbutton关闭按钮:(自定义类:CCustomTabButton) 2.多个tab按钮用QHboxLayout做容器: 3. ...