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

Const vs. Readonly的更多相关文章

  1. C#基础知识七之const和readonly关键字

    前言 不知道大家对const和readonly关键字两者的区别了解多少,如果你也不是很清楚的话,那就一起来探讨吧!探讨之前我们先来了解静态常量和动态常量. 静态常量 所谓静态常量就是在编译期间会对变量 ...

  2. const 与 readonly知多少

    原文地址: http://www.cnblogs.com/royenhome/archive/2010/05/22/1741592.html 尽管你写了很多年的C#的代码,但是可能当别人问到你cons ...

  3. [c#基础]关于const和readonly常见的笔试题剖析

    引言 有那么几天没更新博客了,发现到了不得不写的地步,总是有那么个声音在强迫自己,虽然工作很累,但是有些东西不写出来,不能原谅自己.今天为什么总结这两个关键字的区别,总觉得这两个关键字的用法用的太习惯 ...

  4. const 和 readonly

    const 和 readonly 的异同 Const readonly 字面意 不变常量,不可修改 只读操作,不可写 初始化 必须在声明的同时赋值 可在声明和构造方法中进行赋值 所属关系 类.即sta ...

  5. C#夯实基础系列之const与readonly

    一.const与readonly的争议       你一定写过const,也一定用过readonly,但说起两者的区别,并说出何时用const,何时用readonly,你是否能清晰有条理地说出个一二三 ...

  6. 读书笔记:const和readonly、static readonly 那些事

    C#中表示不变的量(常量)的两种形式:const 和readonly const 是静态常量 readonly 是动态常量 严格的来讲:const 应该称为常量 而readonly 则应称为只读变量. ...

  7. const和readonly区别

    内容来源<<你必须知道的.NET>>(转载) 标题:什么才是不变:const和readonly 内容: const:用 const 修饰符声明的成员叫常量,是在编译期初始化并嵌 ...

  8. [转]const 与 readonly知多少

    引自:http://www.cnblogs.com/royenhome/archive/2010/05/22/1741592.html 尽管你写了很多年的C#的代码,但是可能当别人问到你const与r ...

  9. const与readonly深度分析(.NET)

    前言 很多.NET的初学者对const和readonly的使用很模糊,本文就const和readonly做一下深度分析,包括: 1. const数据类型的优势 2. const数据类型的劣势 3. r ...

  10. [c#] const 与 readonly

    c# 中 const 与 readonly 关键字看似相同,实则不同.重点在于确定值的时间. const const 很简单,就是一个常量,不可以被 static 修饰,因为被 const 修饰的字段 ...

随机推荐

  1. js中的json的小例子

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xht ...

  2. 小程序通过用户授权获取手机号之getPhoneNumber

    小程序有一个获取用户很便捷的api,就是通过getPhoneNumber获取用户的已经绑定微信的手机号码.有一点要大家注意,现在微信和注重用户体验,有些方法都是需要用户主动去触发才能调用的,比如get ...

  3. JSz中的静态方法和实例方法的分析

    我又回来了,最近忙着喝枸杞,没来写博客感觉很有负罪感,今晚我来写一点小小的知识点 可能我们在用形如Array.of()的方法时会产生一些疑问,为什么我们能不实例化直接使用Array上的of()方法呢, ...

  4. 全局eslint不生效的处理

    react项目里能用上 eslint 的 airbnb 规范真是的,对自己的编码有很好的帮助,不经可以养成良好的代码风格,而且还能检测出 state或者变量 是否 使用过, 然而,所在团队的小伙伴们, ...

  5. CSS实现DIV从隐藏到展示的过渡效果

    CSS中有很多功能强大的方法,其中过渡属性transition就很牛叉.你不用写一行JavaScript代码,随便写点css就可以实现一个动画效果.下面结合我在W3C网站上看到的实例,举个栗子说明下( ...

  6. LeetCode题解之Leaf-Similar Trees

    1.题目描述 2.问题分析 将叶子节点的值放入vector,然后比较. 3.代码 bool leafSimilar(TreeNode* root1, TreeNode* root2) { vector ...

  7. python 流程控制(for循环语句)

    1,for循环基本语法 2, for循环常用序列 3,for循环 else使用方法 1,for循环基本语法 for iterating_var in sequence: statements(s) 2 ...

  8. 俩表之间的添加Sql

    insert into 表3(字段1,字段2) select  表1.UserName,表2.GroupName  from 表1,表2 where ...

  9. 分享一个基于小米 soar 的开源 sql 分析与优化的 WEB 图形化工具

    soar-web 基于小米 soar 的开源 sql 分析与优化的 WEB 图形化工具,支持 soar 配置的添加.修改.复制,多配置切换,配置的导出.导入与导入功能. 环境需求 python3.xF ...

  10. MySQL自带的性能压力测试工具mysqlslap

    mysqlslap是从MySQL的5.1.4版开始就开始官方提供的压力测试工具. 通过模拟多个并发客户端并发访问MySQL来执行压力测试,同时提供了较详细的SQL执行数据性能报告,并且能很好的对比多个 ...