比较const ,readonly, stitac readonly
比较const ,readonly, stitac readonly:
- const和readonly的值一旦初始化则都不再可以改写;
- const必须在声明时初始化;readonly既可以在声明时初始化也可以在构造器中初始化,因为见4;
- const隐含static,不可以再写static const;readonly则不默认static,如需要可以写static readonly;
- const是编译期静态解析的常量(因此其表达式必须在编译时就可以求值);readonly则是运行期动态解析的常量,static readonly字面就是实例时赋值;
- const既可用来修饰类中的成员,也可修饰函数体内的局部变量;readonly只可以用于修饰类中的成员。
- const访问必须以"Classname.VariableName"方式访问,而readonly访问必须以"InstanceName.VariableName"方式访问。
代码说明问题
const关键字
class ConstantEx
{
public const int number =;
} class Program
{
static void Main(string[] args)
{
//如果这里使用 ConstantEx.number=10 会出错,在整个app内该值不可改变
Console.WriteLine(ConstantEx.number);// 类名+常量访问
Console.ReadLine();
}
}
readonly关键字
class ReadOnlyEx
{
//说明该值可以在运行时改变,即在实例化时改变该值
public readonly int number = ;
public ReadOnlyEx()
{
number =;
}
public ReadOnlyEx(bool IsDifferentInstance)
{
number = ;
}
} class Program
{
static void Main(string[] args)
{
//必须实例化后访问
ReadOnlyEx readOnlyInstance = new ReadOnlyEx();
Console.WriteLine(readOnlyInstance.number); ReadOnlyEx differentInstance = new ReadOnlyEx(true);
Console.WriteLine(differentInstance.number); Console.ReadLine();
}
}
比较const ,readonly, stitac readonly的更多相关文章
- 到底是 const 还是 static readonly
真的一样? const 和 static readonly 常在程序中用来声明常量,调用方法也没有什么不同,他们真的一样吗?我们可以做个试验. 程序集内的常量 现在我们建立一个程序,里面有一个MyCl ...
- 【转】const和static readonly
我们都知道,const和static readonly的确很像:通过类名而不是对象名进行访问,在程序中只读等等.在多数情况下可以混用.二者本质的区别在于,const的值是在编译期间确定的,因此只能在声 ...
- const, static and readonly
const, static and readonly http://tutorials.csharp-online.net/const,_static_and_readonly Within a cl ...
- (C#) What is the difference between "const" and "static readonly" ?
const int a must be initialized initialization must be at compile time readonly int a can use defaul ...
- const 还是 static readonly
到底是 const 还是 static readonly 真的一样? const 和 static readonly 常在程序中用来声明常量,调用方法也没有什么不同,他们真的一样吗?我们可以做个试 ...
- const和static readonly的区别
我们都知道,const和static readonly的确很像:通过类名而不是对象名进行访问,在程序中只读等等. 在多数情况下可以混用.二者本质的区别在于,const的值是在编译期间确定的,因此只能在 ...
- C# const和static readonly区别
[转]C# const和static readonly区别 以前只是知道Const和static readonlyd的区别在于const的值是在编译期间确定的,而static readonly是在运行 ...
- readonly=“readonly”与readonly=“true”
<input id="u" readonly /> <input id="u" readonly="readonly" / ...
- Const(常量)与readonly(只读)的区别
const与readonly定义的值都不能更改,但它们到底有哪些异同点呢? Const ² Const是常量的意思,其定义的变量只能读取不能更改,且只能在定义时初始化,不能在构造函数与其它属性与方法中 ...
随机推荐
- Discuz x2.5 单页制作的教程
首先,单页包括该单页的php文件和该单页的模板(.htm)文件,比如:host.php.host.htm 单页的php文件内容如下: <?php require './source/class/ ...
- xshell连接linux服务器切换至oracle的sqlplus控制台时,无法使用回车键的解决方案!
当使用xshell连接linux服务器后,切换至sqlplus控制台,当使用回车键时,出现^H的符号,貌似回车键不能用!解决方案: 1)$ stty erase ^H : 与退格键相关的设置是eras ...
- Cocos2d-x第一个坑,NDK 编译环境
这些天搭建windows cocos2d-x的环境,基本上崩溃到死.目前好转.终于可以编译通过: 生成模板工程:在cmd下进入cocos2d-x的主目录,D:\Android\cocos2d-x-2. ...
- 九度OJ - 题目1481:Is It A Tree?
题目描述: A tree is a well-known data structure that is either empty (null, void, nothing) or is a set o ...
- PS 颜色表大全-CMYK颜色表(2)
CMYK颜色表 编号 C M Y K R G B 16进制值 1 0 100 100 45 139 0 22 8B0016 2 0 100 100 25 178 0 31 B2001F 3 0 100 ...
- approval workflow in sharepoint designer
http://office.microsoft.com/en-us/sharepoint-designer-help/video-create-an-approval-workflow-in-shar ...
- lua通过bunlde读注意事项
把lua脚本做成bundle,加载字符串,变成lua对象: lua loadstring("name")() 注意:loadstring的问题: 无法访问全局local变量,需要改 ...
- 【noi2013】【bz3244】树的计数
题目概括:给出树的dfs.bfs序 求树的期望高度 题解:由于我比较懒 先copy一段百度文库的题解~void copy(){我们可以发现,所求的树之所以会有很多种,是因为出现了这种情况:对于A.B, ...
- 深入js的面向对象学习篇(继承篇)——温故知新(三)
写这篇有关继承的文章时,突然想起,几天前的面试.因为习惯在学习知识的时候加上自己的理解,很喜欢用自己话来解释,于是乎当面试被问起继承原理时,噼里啪啦一大堆都是自己组织的话,(也可能是因为个人紧张.外加 ...
- HTML可编辑下拉框
<div style="position:relative;"> <select style="width:120px;" onchang ...