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 -
Zero
etc 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 修饰的字段 ...
随机推荐
- 面向对象设计模式_享元模式(Flyweight Pattern)解读
场景:程序需要不断创建大量相似的细粒度对象,会造成严重的内存负载.我们可以选择享元模式解决该问题. 享元抽象:Flyweight 描述享元的抽象结构.它包含内蕴和外蕴部分(别被术语迷惑,这是一种比较深 ...
- NIO学习笔记五:Buffer 的使用
Java NIO中的Buffer用于和NIO通道进行交互.数据是从通道读入缓冲区,从缓冲区写入到通道中. 缓冲区本质上是一块可以写入数据,然后可以从中读取数据的内存.这块内存被包装成NIO Buffe ...
- JavaScript高级编程——Date类型
JavaScript高级编程——Date类型 <!DOCTYPE html> <html xmlns="http://www.w3.org/1999/xhtml" ...
- 数据库批量操作中SqlParameter参数传递的问题
数据库批量操作 比如会写:update T_AdminUsers set IsEnabled=@IsEnabled where Id in (@ids) 然后再SqlParameter("@ ...
- Android--自定义半圆环型进度(带动画)
package com.newair.ondrawtext; import android.animation.ValueAnimator; import android.annotation.Tar ...
- [Android] 修图工具Draw9patch使用小结(附ubuntu快捷截图方法)
做项目的时候,素材图遇到点问题,然后老大大概给我讲了讲android下面图片格式.9.png和draw 9-patch的用法,感觉很清楚也很有用,所以记录一下. 原文地址请保留http://www.c ...
- (个人记录)Python2 与Python3的版本区别
现在还有些开源模块还没有更新到python3 ,不了解版本区别,无法对不合适的地方进行更改. 由于只追求向Python3靠近,所以对于python2的特别用法不探究. 此文不补全所有版本区别,仅作档案 ...
- maven学习笔记--maven项目创建
使用Maven命令和Eclipse的Maven插件,创建Maven项目 (1)maven命令生成项目 新建一个文件目录,dos进入该目录并执行下面命令: mvn archetype:c ...
- tomcat上传内容报错
公司项目报错: o.s.boot.web.support.ErrorPageFilter : Forwarding to error page from request ...
- python的学习之路day3
大纲 1.汉字编码 2.浅拷贝与深拷贝 3.函数 4.return 5.函数的基本参数 6.format 7.三元运算(三目运算) 8.python的内置函数 abs() all() any() bo ...