Usage of readonly and const
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.
- For value type variable, it’s value can not be changed.
- 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的更多相关文章
- 我所理解的readonly和const
最近要给学校软件小组新成员讲几次课,所以把很多以前懒得学习的和模糊不清的知识点,重新学习了一下. MSDN是这样解释的: readonly 关键字与 const 关键字不同. const 字段只能在该 ...
- readonly与const
readonly与const 在C#中,readonly 与 const 都是定义常量,但不同之处在于:readonly 是运行时常量,而 const 是编译时常量. ; public void Te ...
- 配置文件App.config的使用以及Readonly与Const的对比
以前我们学习的时候都把连接数据库的连接字符串写在一个类中,因为我们的数据库都在自己电脑上.如果更换数据库地址,需要更改这个类,然后重新编译才可以连接到数据库.现在我们需要将连接字符串当道一个文件中,然 ...
- Readonly与const初识
对于readonly和const,很多人无法具体区分,不清楚它们的具体使用场合:现在我们分析它们之间的区别和使用场合. const是一个编译期常量:const只能用于修饰基元类型.枚举类型或者字符串类 ...
- readonly和const的区别
readonly与const的区别1.const常量在声明的同时必须赋值,readonly在声明时可以不赋值2.readonly只能在声明时或在构造方法中赋值(readonly的成员变量可以根据调用不 ...
- c#:readonly与const的区别
readonly与const的区别: 1.初始化:const 字段只能在该字段的声明中初始化. readonly 字段可以在声明或构造函数中初始化. 2.值: const 字段是编译时常量(con ...
- C#中的readonly跟const用法小结
总结一下常量和只读字段的区别: 由来: 笔者也是在看欧立奇版的<.Net 程序员面试宝典>的时候,才发现自己长久以来竟然在弄不清出两者的情况下,混用了这么长的时间.的确,const与rea ...
- [C#] readonly vs const
C#中的readonly和const两个关键字都可以用来定义系统变量,那两者之间有什么区别呢? 1. const变量赋值后,就不可以对其进行修改.且在定义时就需要给它赋值,使用const修饰的变量是s ...
- 编写高质量代码改善C#程序的157个建议——建议6: 区别readonly和const的使用方法
建议6: 区别readonly和const的使用方法 很多初学者分不清readonly和const的使用场合.在我看来,要使用const的理由只有一个,那就是效率.但是,在大部分应用情况下, “效率” ...
随机推荐
- UVA 11090 Going in Cycle!! 环平均权值(bellman-ford,spfa,二分)
题意: 给定一个n个点m条边的带权有向图,求平均权值最小的回路的平均权值? 思路: 首先,图中得有环的存在才有解,其次再解决这个最小平均权值为多少.一般这种就是二分猜平均权值了,因为环在哪也难以找出来 ...
- 【原创】回溯线搜索 Backtracking line search
机器学习中很多数值优化算法都会用到线搜索(line search).线搜索的目的是在搜索方向上找到是目标函数\(f(x)\)最小的点.然而,精确找到最小点比较耗时,由于搜索方向本来就是近似,所以用较小 ...
- WdatePicker的一些用法
在选择日期之后,再执行自己的另一个函数 onpicked: function () { LoadData(); } <input type="text" class=& ...
- 应用emailAutoComplete.js来自动显示邮箱后缀列表
我们经常有邮箱的人都特别清楚,在输入我们的邮箱时,会自动显示出邮箱后缀列表,这个用户体验是不错的. 操作据悉——当我们输入文字时,会自动有个邮箱后缀名的列表. 而我这边的代码是,应用jque ...
- Mysql 的存储过程和存储函数
优点: v 提高安全性 v 简化mysql查询 v 减轻带宽负担 缺点: v 增加服务器负担 v 对我们开发者来说,难度大一点 PHP中的函数 Function funname(参数){ //函数体 ...
- Matlab编程实例(3) 函数向左或向右平移N点 左移右移
%函数移动 close all; clear all; dir=input('请输入平移方向,“1”为向左,“2”为向右'); if dir~=1&&dir~=2;%输入控制 e ...
- mysql DDL语句
sql语言分为三个级别. 1.ddl 语句 ,数据定义语句,定义了数据库.表.索引等对象的定义.常用语句包含:create.drop.alter. 2.dml 语句 ,数据操纵语句,用于添加.删除.更 ...
- HDU 5730 Shell Necklace cdq分治+FFT
题意:一段长为 i 的项链有 a[i] 种装饰方式,问长度为n的相连共有多少种装饰方式 分析:采用dp做法,dp[i]=∑dp[j]*a[i-j]+a[i],(1<=j<=i-1) 然后对 ...
- C#条件语句、循环语句
一.程序的三种结构 顺序结构 分支结构 循环结构 二.条件语句if 语句是最有用的控制结构之一. if … else …语句的语法: if (布尔表达式)执行操作的语句 或if (布尔表达式)执行操 ...
- 树形DP CCPC网络赛 HDU5834 Magic boy Bi Luo with his excited tree
// 树形DP CCPC网络赛 HDU5834 Magic boy Bi Luo with his excited tree // 题意:n个点的树,每个节点有权值为正,只能用一次,每条边有负权,可以 ...