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. VM虚拟机克隆_修改网络

    1.如果网络中没有VMware的网卡,记得重置即可 2.如果右上角没有了网络图标,直接 server NetworkManager restart 3.网络配置 1)在/etc/sysconfig/n ...

  2. POJ3268(KB4-D spfa)

    Silver Cow Party Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 23426   Accepted: 1069 ...

  3. js-ES6学习笔记-module(2)

    1.如果想为输入的变量重新取一个名字,import命令要使用as关键字,将输入的变量重命名. import { lastName as surname } from './profile'; 2.im ...

  4. Win7/8/10十个最强大通用快捷键

    Windows 操作系统功能丰富,可视化的界面能够帮助我们提高工作效率.不过,日常使用中,很多用户都习惯“一只鼠标走天下”,频繁的点击让手指疲惫不堪. 为此,我们总结了十个适用于 Windows 7 ...

  5. Nginx的性能优化

    1.优化worker进程个数: 在高并发.高访问量的WEB服务场景,需要事先启动更多的nginx进程,以保证快速响应并处理大量并发用户的请求,优化nginx进程个数的配置项就是,在nginx.conf ...

  6. Cobalt Strike 学习

    前言 本文以一个模拟的域环境为例对 Cobalt Strike 的使用实践一波. 环境拓扑图如下: 攻击者(kali) 位于 192.168.245.0/24 网段,域环境位于 192.168.31. ...

  7. 华Xia相机WEB后台设置

    1:设备维护->设备信息:出入口类型:入口2:更多参数->屏显参数:屏显协议18 3:更多参数->外设参数:RS581-1 串口使能勾上 串口工作模式 屏显控制 96004:更多参数 ...

  8. python的函数(二)

    1,函数的变量 2,函数的返回值 1,函数的变量 1.0,函数的变量分为局部变量和全局变量. def fun(): x = 100 print x 这个x是局部变量,函数执行完后,x的变量就会销毁,只 ...

  9. CreateProjectFormat——初始项目目录格式

    百度云链接:https://pan.baidu.com/s/1f_2YaMf9619Rt5AlC6nryQ 密码:v2cb 修改中...

  10. tp5 migrate数据库迁移工具

    tp5相对与tp3.2有很大的不同 migrate是其中一点,通过migrate程序员可以在php代码中创建数据库修改回滚等操作 首先下载migrate扩展,命令行到当前项目目录下执行 compose ...