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的理由只有一个,那就是效率.但是,在大部分应用情况下, “效率” ...
随机推荐
- vs2010 js代码折叠
方法一:插件 在Visaul Studio 2010中写js或css代码,缺少像写C#代码时的那种折叠功能,当代码比较多时,就很不方便. 但是已经有VS2010扩展支持这个功能,它就是--JSEn ...
- Repeater 控件 当数据源没有数据的时候显示 暂无数据 的两种方式
第一种:现在前台给Repeater控件外面的div加一个runat=”server” 然后在cs后台判断数据源是否为空, 是的话就修改这个前台div的InnerText或者是InnerHtml 即可 ...
- apache开源项目-- UIMA
UIMA (Unstructured Information Management applications) 是一个软件系统,用来分析大量的非结构化信息从而发掘中对最终用户有用的知识点,一个最典型的 ...
- 她让我懂得了怎样学习Flash
原文:http://www.asv5.cn/blog/article.asp?id=169 最近忙着寻找两样丢失了很久的东西,都是她帮我找回来的,第一样叫做自信,第二样叫做梦想.也正因为有了她,我才从 ...
- Eclipse小技巧<一>
Eclipse是一款特别好用的开源开发工具,基于插件的特性使其能够进行各种语言的开发.非常喜欢eclipse里的编码风格,感觉这个开发工具十分灵活,又有很多开发的小技巧能够提高开发效率,每次学到一个t ...
- Android-AnimationDrawable(三)运行的几种方式
项目开发用到了AnimationDrawable,调用start后没有运行,很纳闷.google搜了下.记录一下. 这个AnimationDrawable.start不能直接写在onClick,onS ...
- Color Length
题意: 给出两个字符串,求把两字符串组成一个字符串使的字符串中的相同字母的最远距离的和最小. 分析: 本题关键在于怎么计算距离和的方法上.dp[i][j]表示处理到长度i的a串,长度j的b串还需要的计 ...
- codeforces 260 div2 B题
打表发现规律,对4取模为0的结果为4,否则为0,因此只需要判断输入的数据是不是被4整出即可,数据最大可能是100000位的整数,判断能否被4整出不能直接去判断,只需要判断最后两位(如果有)或一位能否被 ...
- 常用sql命令
--1) 创建一张学生表,包含以下信息,学号,姓名,年龄,性别,家庭住址,联系电话 CREATE TABLE student ( [id] [int] IDENTITY(1,1) NOT NU ...
- CSS基础知识—【结构、层叠、视觉格式化】
结构和层叠 选择器的优先级顺序: style[内联元素]选择器>Id选择器>类选择器 属性选择器>元素选择器>通配器选择器 重要性:@important 有这个标记的属性值,优 ...