总结一下const和readonly
- const和readonly的值一旦初始化则都不再可以改写;
- const只能在声明时初始化;readonly既可以在声明时初始化也可以在构造器中初始化;
- const隐含static,不可以再写static const;readonly则不默认static,如需要可以写static readonly;
- const是编译期静态解析的常量(因此其表达式必须在编译时就可以求值);readonly则是运行期动态解析的常量;
- const既可用来修饰类中的成员,也可修饰函数体内的局部变量;readonly只可以用于修饰类中的成员
前面是我从网上摘录的,文字太多,懒得自己再总结和打字了.
注意,第四点尤为重要,我用以下代码来说明和验证:
1.建立文本文件,然后改成.cs文件-lib.cs:
- 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;
- }
- }
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;
}
}
- 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();
- }
- }
- }
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();
}
}
}
- 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;
- }
- }
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;
}
}

- .field public static literal float64 PI = float64(3.1415000000000002)
.field public static literal float64 PI = float64(3.1415000000000002)
- .field public static initonly float64 pi
.field public static initonly float64 pi
明显lib.cs被编译成元数据和IL汇编时,PI直接被替换成float64(3.1415000000000002)
而.cctor: void()静态构造函数中,
- .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
.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函数中我们到底做了些什么:
- .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
.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时,
- IL_0001: ldc.r8 3.1400000000000001
- 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时,
- 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的更多相关文章
- C#基础知识七之const和readonly关键字
前言 不知道大家对const和readonly关键字两者的区别了解多少,如果你也不是很清楚的话,那就一起来探讨吧!探讨之前我们先来了解静态常量和动态常量. 静态常量 所谓静态常量就是在编译期间会对变量 ...
- const 与 readonly知多少
原文地址: http://www.cnblogs.com/royenhome/archive/2010/05/22/1741592.html 尽管你写了很多年的C#的代码,但是可能当别人问到你cons ...
- [c#基础]关于const和readonly常见的笔试题剖析
引言 有那么几天没更新博客了,发现到了不得不写的地步,总是有那么个声音在强迫自己,虽然工作很累,但是有些东西不写出来,不能原谅自己.今天为什么总结这两个关键字的区别,总觉得这两个关键字的用法用的太习惯 ...
- const 和 readonly
const 和 readonly 的异同 Const readonly 字面意 不变常量,不可修改 只读操作,不可写 初始化 必须在声明的同时赋值 可在声明和构造方法中进行赋值 所属关系 类.即sta ...
- C#夯实基础系列之const与readonly
一.const与readonly的争议 你一定写过const,也一定用过readonly,但说起两者的区别,并说出何时用const,何时用readonly,你是否能清晰有条理地说出个一二三 ...
- 读书笔记:const和readonly、static readonly 那些事
C#中表示不变的量(常量)的两种形式:const 和readonly const 是静态常量 readonly 是动态常量 严格的来讲:const 应该称为常量 而readonly 则应称为只读变量. ...
- const和readonly区别
内容来源<<你必须知道的.NET>>(转载) 标题:什么才是不变:const和readonly 内容: const:用 const 修饰符声明的成员叫常量,是在编译期初始化并嵌 ...
- [转]const 与 readonly知多少
引自:http://www.cnblogs.com/royenhome/archive/2010/05/22/1741592.html 尽管你写了很多年的C#的代码,但是可能当别人问到你const与r ...
- const与readonly深度分析(.NET)
前言 很多.NET的初学者对const和readonly的使用很模糊,本文就const和readonly做一下深度分析,包括: 1. const数据类型的优势 2. const数据类型的劣势 3. r ...
- [c#] const 与 readonly
c# 中 const 与 readonly 关键字看似相同,实则不同.重点在于确定值的时间. const const 很简单,就是一个常量,不可以被 static 修饰,因为被 const 修饰的字段 ...
随机推荐
- Ruby require 路径问题
require 负责引用一个外部文件,可以省略".rb"字样. 如: require 'foo.bar' 等价于 require 'foo' 在Ruby中,同一目录下的文件, 如 ...
- [NOIP1999]拦截导弹
1999年NOIP全国联赛提高组 题目描述 Description 某国为了防御敌国的导弹袭击,发展出一种导弹拦截系统.但是这种导弹拦截系统有一个缺陷:虽然它的第一发炮弹能够到达任意的高度,但 ...
- C程序设计 习题之1-20 detab
码农一定是最需要动手实操的行业之一.有一句话叫,好记性不如烂笔头,牵强附会引申到这里,变成看书百遍,不如码字运行一遍.是不是有那么一点味道?哈哈! 这几天看的<C程序设计>,看完每个章节还 ...
- Clean Code – Chapter 3: Functions
Small Blocks and Indenting The blocks within if statements, else statements, while statements, and s ...
- linux下mysql的表名问题
最近从win转移到了linux,在本机跑好的程序但在linux下一个SQL语句报了错误,发现是表名未找到,错误原因是在linux下mysql的表名是严格区分大小写的.. MYSQL在LINUX下数据库 ...
- JavaScript Garden
Objects Object Usage and Properties Everything in JavaScript acts like an object, with the only two ...
- 正则匹配:Email 密码强度 身份证 手机号 日期 数字每4个字空一格等
正则表达式,一个十分古老而又强大的文本处理工具,仅仅用一段非常简短的表达式语句,便能够快速实现一个非常复杂的业务逻辑.熟练地掌握正则表达式的话,能够使你的开发效率得到极大的提升.下面是在前端开发中经常 ...
- Wix#可以直接写C#代码来生成Wix的MSI安装文
博客搬到了fresky.github.io - Dawei XU,请各位看官挪步.最新的一篇是:Wix#可以直接写C#代码来生成Wix的MSI安装文.
- Win7与虚拟机VMware下运行的Ubuntu共享文件夹
安装VMware Tools,在VMware面板上选择“虚拟机-重新安装VMware tools…”,如下图所示: 在这里VMware虚拟了一个光盘镜像,我们需要把这个镜像挂载到本机的/mnt目录下面 ...
- nginx安装php和php-fpm
最近在学习nginx,看了好多帖子终于安装成功了. 经验,首先不要用yum安装,安装完以后根本找不到安装目录在哪里呀,然后安装失败以后会很不方便. 最终选择了自己编译安装. 看了好多帖子都不行,终于找 ...