Just4Fun - Comparaison between const and readonly in C#
/* By Dylan SUN */
Today let us talk about const and readonly.
- const is considered as compile-time constant
- readonly is considered as runtime constant.
I will demonstrate some example code to clarify their usages and differences.
I. Const usages
Firstly, you can see that const variable can be declared and assigned at class level.
public class ConstExample
{
public const string CstValue1 = "hello"; //Correct
}
You can also declare a const variable in constructor
public ConstExample()
{
const string cstValue6 = "hehe"; //Correct
}
You can see that const can be declared at method level too
public void Display()
{
const string cstValue2 = "cst 2"; //Correct : Constant can be declared in method
}
If you want to assign another value to CstValue1, you will get an compile-time exception. Because const can only be assigned when declaration.
CstValue1 = "hello2"; //Compile time exception : Can not resolve symbol CstValue1
You can not also assign another value to CstValue1 in the constructor. You will get an compile-time exception.
public ConstExample()
{
CstValue1 = "hello2"; //Compile time exception : Constant can not be used as an assignment target
}
May be you want to assign a normal string variable to a const like this. But you will see it doesn’t work too.
public string v1;
const string CstValue2 = v1; //Compile time exception : Constant initializer must be compile-time constant
But you can assign the operation result of several consts to another const like this.
public const string CstValue3 = "world"; //Correct
public const string CstValue4 = CstValue1 + CstValue3; //Correct : Constants can only be assigned with values or with consts
You can also declare a const, and assign it with the addition of two consts in the method.
const string cstValue5 = CstValue1 + CstValue3;
When you display them you can see
Console.WriteLine(CstValue1); //=> hello
Console.WriteLine(cstValue2); //=> cst 2
Console.WriteLine(CstValue4); //=> helloworld
Console.WriteLine(cstValue5); //=> helloworld
II. readonly usages
Firstly, you can declare an readonly variable at class level.
public class ReadOnlyExample
{
public readonly string rdValue1 = "good";
}
You can also just declare a readonly variable without assigning any value to it.
public readonly string rdValue2;
You can not declare a readonly variable in a method.
readonly string rdValue6 = "hohoho"; //compile time exception : statement expected
You cannot assign a value to variable rdValue1 after the declaration, except for, you assign a value in the class constructor.
rdValue1 = "good 2"; //Compile time exception : Can not resolve symbol rdValue1
You can not assign a readonly variable to another readonly variable at class level.
readonly string rdValue3 = rdValue1; //Compile time exception : cannot access non-static field 'rdValue1' in static context
You can not assign a normal variable to a readonly variable neither at class level.
public string value1 = "hey";
readonly string rdValue6 = value1; //Compile time exception : cannot access non-static field 'value1' in static context
You can not assign value to a readonly variable in a method.
public void Display()
{
rdValue1 = "good one"; //Compile time exception: Readonly field can not be used as an assignment target
rdValue2 = "good too"; //Compile time exception: Readonly field can not be used as an assignment target
}
But you can assign a normal variable to a readonly variable in class constructor.
You can even assign the operation result of several readonly variables to another readonly variable.
public readonly string rdValue4;
public readonly string rdValue5;
public ReadOnlyExample(string value)
{
rdValue2 = value; //Correct
rdValue4 = rdValue1 + rdValue2; //Correct : Assign another readonly variable to another readonly variable can only be done in constructor
rdValue5 = rdValue1 + value1; //Correct : Assign readonly variable with normal variable to another readonly variable
}
when you display them, you will see
Console.WriteLine(rdValue1); //=> good
Console.WriteLine(rdValue2); //=> day
Console.WriteLine(rdValue4); //=> goodday
Console.WriteLine(rdValue5); //=> goodhey
If you want to access to a const, you must access it via the class
Console.WriteLine(ConstExample.CstValue1); //Const variable can only be accessed by class
If you want to access to a readonly variable, you must access it via an instance of class.
var re = new ReadOnlyExample("day");
Console.WriteLine(re.rdValue1); //Readonly value can only be accessed by instance of class
So to conclude,
Const :
Can only be assigned at declaration
Can be declared at class level, in constructor and in method.
Can be assigned with operation result of several consts at class
level or in constructor or in method. (like addition, multiplication
etc)Once the declaration is done, you can never modify a const’s value, neither in constructor, nor just after declaration.
You can access const variable only by class.
Readonly:
Can only be assigned at declaration or in constructor.
Can be declared only at class level. You can declare a readonly
variable neither in constructor nor in method.Can be assigned with operation result of several readonly variables only in constructor (like addition, multiplication etc). You can not assign them to a readonly variable when declaration or in a method.
You can access Readonlyvariable only by the instance of class.
I hope you find this article helpful!
版权声明:本文博主原创文章,博客,未经同意不得转载。
Just4Fun - Comparaison between const and readonly in C#的更多相关文章
- C#基础知识七之const和readonly关键字
前言 不知道大家对const和readonly关键字两者的区别了解多少,如果你也不是很清楚的话,那就一起来探讨吧!探讨之前我们先来了解静态常量和动态常量. 静态常量 所谓静态常量就是在编译期间会对变量 ...
- const 与 readonly知多少
原文地址: http://www.cnblogs.com/royenhome/archive/2010/05/22/1741592.html 尽管你写了很多年的C#的代码,但是可能当别人问到你cons ...
- [c#基础]关于const和readonly常见的笔试题剖析
引言 有那么几天没更新博客了,发现到了不得不写的地步,总是有那么个声音在强迫自己,虽然工作很累,但是有些东西不写出来,不能原谅自己.今天为什么总结这两个关键字的区别,总觉得这两个关键字的用法用的太习惯 ...
- const 和 readonly
const 和 readonly 的异同 Const readonly 字面意 不变常量,不可修改 只读操作,不可写 初始化 必须在声明的同时赋值 可在声明和构造方法中进行赋值 所属关系 类.即sta ...
- C#夯实基础系列之const与readonly
一.const与readonly的争议 你一定写过const,也一定用过readonly,但说起两者的区别,并说出何时用const,何时用readonly,你是否能清晰有条理地说出个一二三 ...
- 读书笔记:const和readonly、static readonly 那些事
C#中表示不变的量(常量)的两种形式:const 和readonly const 是静态常量 readonly 是动态常量 严格的来讲:const 应该称为常量 而readonly 则应称为只读变量. ...
- const和readonly区别
内容来源<<你必须知道的.NET>>(转载) 标题:什么才是不变:const和readonly 内容: const:用 const 修饰符声明的成员叫常量,是在编译期初始化并嵌 ...
- [转]const 与 readonly知多少
引自:http://www.cnblogs.com/royenhome/archive/2010/05/22/1741592.html 尽管你写了很多年的C#的代码,但是可能当别人问到你const与r ...
- const与readonly深度分析(.NET)
前言 很多.NET的初学者对const和readonly的使用很模糊,本文就const和readonly做一下深度分析,包括: 1. const数据类型的优势 2. const数据类型的劣势 3. r ...
随机推荐
- JSTL解析——002——core标签库01
javaEE5之前的版本需要引用JSTL相关的jar包.tld文件等,JAEE5之后就不用这么麻烦了, 如果你的还是不能使用就去官网下载(jstl.jar和standard.jar)这两个jar包,将 ...
- STM8S 串口应用 UART2 STM8S105
//少说话.多做事,下面是我验证过没有问题的串口发送接受数据 //使用MCU stm8s105c6 UART2 //初始化时调用: GPIO_DeInit(GPIOD); /* Configure P ...
- vs2008编译QT开源项目--太阳神三国杀源码分析(三) 皮肤
太阳神三国杀的界面很绚丽,界面上按钮的图标,鼠标移入移出时图标的变化,日志和聊天Widget的边框和半透明等效果,既可以通过代码来控制,也可以使用皮肤文件qss进行控制.下面我们分析一下三国杀的qss ...
- Jar包转成Dll的方式(带嵌套的jar也能做) (转)
研究很好几天,终于成功了.因为写了一个Java的项目,现在要求要改写成C#版本的.但是其中用到了svnkit,svnkit是java平台的.改写成C#的话,要使用SharpSVN,但是SharpSVN ...
- Zero Clipboard - 跨浏览器兼容的“复制到剪贴板”功能
开发中经常会用到复制的功能,在 IE 下实现比较简单,但要想做到跨浏览器比较困难了. 本文将介绍一个跨浏览器的库类 Zero Clipboard ,它利用 Flash 进行复制,所以只要浏览器装有 F ...
- Xcode免证书真机调试,解决cannot read entitlement data问题
本文是根据某个帖子写的(帖子链接在最后放出),但是在配置的过程中,遇到了一个纠结的问题,这个问题折腾了我N久,一直没搞明白到底是什么原因,问题如下: 按照原帖上写的每一步去做了,但是在最后编译的时候出 ...
- Android 实现自己定义多级树控件和全选与反选的效果
博文開始之前,首先要感谢大牛:(lmj623565791),本博文是在其博文http://blog.csdn.net/lmj623565791/article/details/40212367基础上进 ...
- Eclipse 每次打开workspace目录记录位置?
E:\eclipse_j2ee eclipse安装根目录 在这个文件下: E:\eclipse_j2ee\configuration\.settings\org.eclipse.ui.ide.pref ...
- hdu4035(概率dp)
题目连接:http://acm.hdu.edu.cn/showproblem.php?pid=4035 题意:有n个房间,由n-1条隧道连通起来,实际上就形成了一棵树, 从结点1出发,开始走,在每个结 ...
- I2C分析三
1 引言 IIC (Inter-Integrated Circuit1总线是一种由Philips公司开发的2线式串行总线,用于连接微控制器及其外围设备.它是同步通信的一种特殊形式,具有接口线少.控制方 ...