Many new learners can not make sure the usage scenarios of readonly and const keywords. In my opinion, the main reason of using readonly keyword is the efficiency. but in most occasions, efficiency isn’t treated as the high level. so I’m willing to use readonly keyword. because of it’s much more flexibility. The nature differences of these two keywords are as below:

one: const is a compile-time constant, but readonly is a runtime constant.

two: const can only modify the primitive types, enum types and string types, but readonly has no limits.

For the first point, because of the compile-time constant feature, so natively it’s static and you can’t add the static modifier manually.

eg: static const int value = 100;

For the efficiency of const keyword, after compiled by the compiler, the constants where they are used will be replaced by it’s real value.

For the readonly variable , it’s assigned when in runtime, then it’s value can not be changed.

The so-called can not be changed contains two meanings.

  1. For value type variable, it’s value can not be changed.
  2. For reference type variable, it’s reference can not be changed, but the value of it’s referenced instance can be changed.

eg:

class Istone
{ public readonly int age; public Istone(int age)
{ this.age = age;
}
} Istone istone = new Istone(20); istone.age = 300; //it’s wrong, can’t assign value again for readonly variable

  

For the reference variable, after giving an example, you will rapidly understand.

eg:

 class  Istone2
{
public readonly Student stu; public Istone2(Student stu) {
this.stu = stu;
}
} Istone2 stu2 = new Istone2(new Student(){age= 10}); stu2.stu = new Student(){age = 20};//wrong

  

Can not assign value to readonly field, but can change it’s referenced instance like below:

stu2.stu.age = 20;

PS: readonly variable can be initialized, and then assigned value one or more time in the constructor.

Usage of readonly and const的更多相关文章

  1. 我所理解的readonly和const

    最近要给学校软件小组新成员讲几次课,所以把很多以前懒得学习的和模糊不清的知识点,重新学习了一下. MSDN是这样解释的: readonly 关键字与 const 关键字不同. const 字段只能在该 ...

  2. readonly与const

    readonly与const 在C#中,readonly 与 const 都是定义常量,但不同之处在于:readonly 是运行时常量,而 const 是编译时常量. ; public void Te ...

  3. 配置文件App.config的使用以及Readonly与Const的对比

    以前我们学习的时候都把连接数据库的连接字符串写在一个类中,因为我们的数据库都在自己电脑上.如果更换数据库地址,需要更改这个类,然后重新编译才可以连接到数据库.现在我们需要将连接字符串当道一个文件中,然 ...

  4. Readonly与const初识

    对于readonly和const,很多人无法具体区分,不清楚它们的具体使用场合:现在我们分析它们之间的区别和使用场合. const是一个编译期常量:const只能用于修饰基元类型.枚举类型或者字符串类 ...

  5. readonly和const的区别

    readonly与const的区别1.const常量在声明的同时必须赋值,readonly在声明时可以不赋值2.readonly只能在声明时或在构造方法中赋值(readonly的成员变量可以根据调用不 ...

  6. c#:readonly与const的区别

    readonly与const的区别: 1.初始化:const  字段只能在该字段的声明中初始化. readonly  字段可以在声明或构造函数中初始化. 2.值: const 字段是编译时常量(con ...

  7. C#中的readonly跟const用法小结

    总结一下常量和只读字段的区别: 由来: 笔者也是在看欧立奇版的<.Net 程序员面试宝典>的时候,才发现自己长久以来竟然在弄不清出两者的情况下,混用了这么长的时间.的确,const与rea ...

  8. [C#] readonly vs const

    C#中的readonly和const两个关键字都可以用来定义系统变量,那两者之间有什么区别呢? 1. const变量赋值后,就不可以对其进行修改.且在定义时就需要给它赋值,使用const修饰的变量是s ...

  9. 编写高质量代码改善C#程序的157个建议——建议6: 区别readonly和const的使用方法

    建议6: 区别readonly和const的使用方法 很多初学者分不清readonly和const的使用场合.在我看来,要使用const的理由只有一个,那就是效率.但是,在大部分应用情况下, “效率” ...

随机推荐

  1. WinDbg调试命令汇总

    一. 1. !address eax 查看对应内存页的属性 2. vertarget 显示当前进程的大致信息 3 !peb 显示process Environment Block 4. lmvm 可以 ...

  2. 也谈http中get和post

    1.get和post区别: 从设计初衷考虑get是为了查询服务器资源(不改变服务器数据及状态,因此说它是安全和幂等的,但get请求参数一般是直接在url后面,浏览器地址栏中会被看到能保存书签及历史记录 ...

  3. 开学了!这些Linux认证你要知道。

    导读 大家好,今天我们将认识一些非常有价值的全球认可的Linux认证.Linux认证是不同的Linux专业机构在全球范围内进行的认证程序.Linux认证可以让Linux专业人才可以在服务器领域或相关公 ...

  4. [Papers]NSE, $u_3$, Lebesgue space [Cao-Titi, IUMJ, 2008]

    $$\bex u_3\in L^p(0,T;L^q(\bbR^3)),\quad \frac{2}{p}+\frac{3}{q}=\frac{2}{3}+\frac{2}{3q},\quad \fra ...

  5. [Everyday Mathematics]20150129

    计算下列积分 $$\bex \int_a^b (x-a)^2(b-x)^3\rd x. \eex$$

  6. 转载:fstream和ifstream详细用法

    文件 I/O 在C++中比烤蛋糕简单多了.在这篇文章里,我会详细解释ASCII和二进制文件的输入输出的每个细节,值得注意的是,所有这些都是用C++完成的. 一.ASCII 输出 为了使用下面的方法, ...

  7. NSThread 多线程相关

    1.下面的代码,有2点需要注意,1>就是 就是thread:所传得参数,这里传得的是nsarray 当然也可以传其他的类型.2> [self performSelectorOnMainTh ...

  8. 从lighttpd学到的代码技巧

    平时写开脚本,很多时候我们都可以不怎样注意效率,但是看c代码的时候,你会发现,才意思自己真的是一个coder啦 1,单位转换 (根据传入的数返回相应的kb,mb,gb等等) 可能我们直觉来想就会这样做 ...

  9. 依赖包bcrypt安装Issues

    说明:本文在个人博客地址为edwardesire.com,欢迎前来品尝. 在决策树项目中,使用到了bcrypt依赖包来加密文件.在wini8(win7)部署安装这个依赖的时候容易出现出现了问题. 解决 ...

  10. 为了以后愉快的玩耍,Virtualbox安装Ubuntu

    为了以后愉快的玩耍,Virtualbox安装Ubuntu 每次安装虚拟机都是总要折腾一下,毕竟不是特别熟悉,几个小细节总要google半天,为了以后能愉快的玩耍.把这些问题都记录下来,免得再折腾. 此 ...