总结一下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 修饰的字段 ...
随机推荐
- Hadoop HDFS的常用命令
1.将目录/root/data/下的item.txt复制到HDFS下的/user/root下: hadoop fs -copyFromLocal /root/data/item.txt itemdat ...
- Codeforces 439 A. Devu, the Singer and Churu, the Joker
这是本人第一次写代码,难免有点瑕疵还请见谅 A. Devu, the Singer and Churu, the Joker time limit per test 1 second memory l ...
- The Shortest Path in Nya Graph
Problem Description This is a very easy problem, your task is just calculate el camino mas corto en ...
- nyoj 找球号三(除了一个数个数为基数,其他为偶数,编程之美上的)
#include<iostream> #include<stdio.h> using namespace std; int main() { int len; while(ci ...
- Spring笔记(四)SpingAOP
需要的Jar包(String3.2) com.springsource.net.sf.cglib-2.2.0.jar // 作用于cglib方式的动态代理 com.springsource.org.a ...
- 关键在封装并发出了帧-IP冲突也无所谓
最近有点走火入魔了!本文所用技术非标准,较真儿者慎入!! 一个局域网内,两台机器拥有同样的IP,可以吗? 这不就是IP地址冲突吗?当然不行! 可是要知道,如果搞点旁门左道,还是可以做到的! 首先要明白 ...
- 8-13-Exercise
链接:夜间活动 昨天的比赛好郁闷.......倒不是因为题目......在快要比赛的时候突然所有的网站都进不去了.......改了半天的DNS & IP......比赛都比了1个多小时才进去. ...
- 浅谈js单例模式
单例模式就是在系统中保存一个实例,就是一个全局变量,在团队开发中,为了实现一些相似的功能,比如不同页面之间的表单验证,可能需求是不一样的,但是呢命名可能一样,这时就会产生冲突,这时候单例模式就能很好的 ...
- Maven管理项目架包
最近一直在用Maven管理项目的架包,从而发现了几个不错的官方或者非官方的网站. http://mvnrepository.com/artifact/com.ning 这个是我刚刚用来找HTTP开发 ...
- Install Package and Software
svn http://tortoisesvn.sourceforge.net/ git https://download.tortoisegit.org/ http://git-for-windows ...