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 修饰的字段 ...
随机推荐
- 盐水的故事(hdu1408)
盐水的故事 Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) Total Subm ...
- 简单的redis缓存操作(get、put)
简单的redis缓存操作(get.put) 本文介绍简单的redis缓存操作,包括引入jedisjar包.配置redis.RedisDao需要的一些工具.向redis中放数据(put).从redis中 ...
- Python os、sys、pickle、json等模块
1.os 所有和操作系统相关的内容都在os模块,一般用来操作文件系统 import os os.makedirs('dirname1/dirname2') # 可生成多层递归目录 os.removed ...
- ActiveReports 报表控件V12新特性 -- RPX报表转换为RDL报表
ActiveReports是一款专注于 .NET 平台的报表控件,全面满足 HTML5 / WinForms / ASP.NET / ASP.NET MVC / WPF 等平台下报表设计和开发工作需求 ...
- iis7 请求的内容似乎是脚本,因而将无法由静态文件处理程序来处理
cmd 输入 C:\Windows\Microsoft.NET\Framework\V4.0.30319\aspnet_regiis -i
- WOSA/XFS PTR Form解析库—xfsptrdata.h
#ifndef _XFSPTRDATA_H_#define _XFSPTRDATA_H_ #include <XFSPTR.H>#include <Windows.h>#inc ...
- 通过 Cobalt Strike 利用 ms14-068
拓扑图 攻击者(kali) 位于 192.168.245.0/24 网段,域环境位于 192.168.31.0/24 网段. 域中有一台 win7 有两张网卡,可以同时访问两个网段,以这台机器作为跳板 ...
- RecyclerView 下拉刷新和加载更多
一.SwipeRefreshLayout实现下拉刷新 1.方法API: setOnRefreshListener(OnRefreshListener):添加下拉刷新监听器 setRefreshing( ...
- 对WebSocket技术的学习与探索(二)
近日重新开始学习WebSocket技术,什么是WebSocket,在<对WebSocket技术的学习与探索(一)>文章中已经说明白了,还没理解可以看看这篇文章http://www.ruan ...
- 8.2,常用模块介绍:sys,os,time,random
sys: 介绍:主要包含涉及python编译器与系统交互的函数. 常用函数: import sys print(sys.argv)#本文件名,已经运行该程序时的参数 #[如在命令窗口中python3 ...