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. [POJ2234]Matches Game

    Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 9297   Accepted: 5365 Description Here ...

  2. 【HTML】Beginner3:ParagraphsEmphasisLine breaks

    1.Paragraphs </p>     distinct blocks of the text     Think of the HTML contents as if it were ...

  3. (转载)链表环中的入口点 编程之美 leecode 学习

    http://www.cnblogs.com/hiddenfox/p/3408931.html 说的很细 /** * Definition for singly-linked list. * clas ...

  4. 非递归实现先序遍历 java leecode 提交

    写完才知道自己学习都是似是而非啊,大家可以也在leecode上提交代码,纯手写,离开eclipse第一种方式:数据结构书上的,使用栈大概思路.1.不断将根节点的左孩子的左孩子直到为空,在这个过程入栈. ...

  5. Stream消息流 和 Stream Grouping 消息流组

  6. MongoDB 入门之查询(find)

    MongoDB 入门之查询(find) 1. find 简介 (1)find的第一个参数决定了要返回哪些文档. 空的查询文档会匹配集合的全部内容.默认就是{}.结果将批量返回集合c中的所有文档. db ...

  7. IOS网络多线程-GCD

    Grand Central Dispatch (GCD)是Apple开发的一个多核编程的解决方法. dispatch queue分成以下三种: 1)运行在主线程的Main queue,通过dispat ...

  8. Oracle数据库字符串连接方法

    转至:http://database.51cto.com/art/201011/232267.htm 和其他数据库系统类似,Oracle字符串连接使用“||”进行字符串拼接,其使用方式和MSSQLSe ...

  9. 杀死当前Excel进程

    说明: 代码编写过程中,有时需要杀死当前Excel进程,而不影响其他Excel进程. 代码如下: 添加引用: //杀死当前进程的API引用 using System.Runtime.InteropSe ...

  10. An existing PostgreSql installation has been found... 的解决

    PostgreSql卸载之后,重新安装时跳出如下信息: Anexisting PostgreSql installation has been found atC:\ProgramFiles\Post ...