Const vs. Readonly
Const
- 被const修饰的变量不能为静态,因为const实际隐式上已经是静态变量。
- const变量在声明时就必须进行初始化,否则会有编译错误。
- const变量的赋值是发生在编译期间
Readonly
- readonly修饰的变量既可以是静态的,也可以是非静态的(实例化的)
- readonly变量可以在声明时赋值,也可以在对应的构造函数中赋值(初始化),如果变量是静态的,可以在静态构造函数中初始化;如果是非静态的,则需要在实例构造函数中初始化
- 因为上面的原因,所以readonly变量是在运行时才进行赋值的
Other Views
const = value assigned at Compile time and unchangeable once established.
readonly = value assigned at run time and unchangeable once established.
static readonly is typically used if the type of the field is not allowed in a const declaration, or when the value is not known at compile time.
Remember that for reference types, in both cases (static and instance) the readonly modifier only prevents you from assigning a new reference to the field. It specifically does not make immutable the object pointed to by the reference.
If the value will never change, then const is fine -
Zeroetc make reasonable consts ;-p Other than that, static properties are more common.
Const values are burned directly into the call-site; this is double edged:
- it is useless if the value is fetched at runtime, perhaps from config
- if you change the value of a const, you need to rebuild all the clients
- but it can be faster, as it avoids a method call...
- ...which might sometimes have been inlined by the JIT anyway
If the value will never change, then const is fine - Zero etc make reasonable consts ;-p Other than that, static properties are more common.
References
- http://weblogs.asp.net/psteele/archive/2004/01/27/63416.aspx
- http://blogs.msdn.com/b/csharpfaq/archive/2004/12/03/what-is-the-difference-between-const-and-static-readonly.aspx
- http://stackoverflow.com/questions/755685/c-static-readonly-vs-const
Const vs. 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 修饰的字段 ...
随机推荐
- ApplicationListener用法
ApplicationListener是spring提供的接口,作用是在web服务器启动时去加载某些程序. 用法: 1.实现ApplicationListener接口,并重写onApplication ...
- java.lang.ExceptionInInitializerError异常
今天在开发的过程中,遇到java.lang.ExceptionInInitializerError异常,百度查了一下,顺便学习学习,做个笔记 静态初始化程序中发生意外异常的信号,抛出Exception ...
- PHP获取本周的每一天的时间
1.PHP获取未来一周的时间 public function getWeek() { for($i=0;$i<7;$i++) { $arr[$i]=date('Y-m-d',strtotime( ...
- 关于网站中Logo部分的写法
由于SEO对网页产生的影响,我们在写html的时候要注意写好三大要素:1.keywords 2.describtion 3.title 我们在写像h1 h2 h3 这种标题的时候尽量要带有网站的名字 ...
- cf1102F. Elongated Matrix(状压dp)
题意 题目链接 Sol \(n \leqslant 16\)可以想到状压 我们可以预处理出任意两行之间每列的最小值以及相邻两列的最小值 然后枚举一个起点,\(f[sta][i]\)表示走过了\(sta ...
- 使用PeopleSoft 异步操作设置步骤
在手机端调PS接口或同步数据到中间库等程序中经常用到异步操作. 在主菜单>PeopleTools>集成代理程序>集成设置中添加了服务和服务操作后需要设置以下3步才能完成异步程序: 1 ...
- 如何通过REST API登录Portal for ArcGIS
Portal for ArcGIS 提供了两种登录方式:OAuth 2.0和传统的token登录方式. OAuth 2.0的登录原理在之前的一篇文章中已经描述,所以今天就不重复了.下面将介绍对于不支持 ...
- Atitit.播放系统规划新版本 and 最近版本回顾 v3 pbf.doc 1 版本11 (ing)41.1 规划h5本地缓存系列 41.2 Android版本app41.3 双类别系统,
Atitit.播放系统规划新版本 and 最近版本回顾 v3 pbf.doc 1 版本11 (ing)4 1.1 规划h5本地缓存系列 4 1.2 Android版本app4 1.3 双类别系统, ...
- singleInstance和singleTask导致startActivityForResult回调失败
先来了解下这两种启动模式: 1.singleInstance,全局唯一,它的实例在全局(即在众多任务栈中)是唯一的,它单独地存在于属于自己的任务栈中,而且这个任务栈没有其他实例. 2.singleTa ...
- tinymce4.x 上传本地图片(自己写个插件)
tinymce是一款挺不错的html文本编辑器.但是添加图片是直接添加链接,不能直接选择本地图片. 下面我写了一个插件用于直接上传本地图片. 在tinymce的plugins目录下新建一个upload ...