比较const ,readonly, stitac readonly:

  1. const和readonly的值一旦初始化则都不再可以改写;
  2. const必须在声明时初始化;readonly既可以在声明时初始化也可以在构造器中初始化,因为见4;
  3. const隐含static,不可以再写static const;readonly则不默认static,如需要可以写static readonly;
  4. const是编译期静态解析的常量(因此其表达式必须在编译时就可以求值);readonly则是运行期动态解析的常量,static readonly字面就是实例时赋值;
  5. const既可用来修饰类中的成员,也可修饰函数体内的局部变量;readonly只可以用于修饰类中的成员。
  6. 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的更多相关文章

  1. 到底是 const 还是 static readonly

    真的一样? const 和 static readonly 常在程序中用来声明常量,调用方法也没有什么不同,他们真的一样吗?我们可以做个试验. 程序集内的常量 现在我们建立一个程序,里面有一个MyCl ...

  2. 【转】const和static readonly

    我们都知道,const和static readonly的确很像:通过类名而不是对象名进行访问,在程序中只读等等.在多数情况下可以混用.二者本质的区别在于,const的值是在编译期间确定的,因此只能在声 ...

  3. const, static and readonly

    const, static and readonly http://tutorials.csharp-online.net/const,_static_and_readonly Within a cl ...

  4. (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 ...

  5. const 还是 static readonly

    到底是 const 还是 static readonly   真的一样? const 和 static readonly 常在程序中用来声明常量,调用方法也没有什么不同,他们真的一样吗?我们可以做个试 ...

  6. const和static readonly的区别

    我们都知道,const和static readonly的确很像:通过类名而不是对象名进行访问,在程序中只读等等. 在多数情况下可以混用.二者本质的区别在于,const的值是在编译期间确定的,因此只能在 ...

  7. C# const和static readonly区别

    [转]C# const和static readonly区别 以前只是知道Const和static readonlyd的区别在于const的值是在编译期间确定的,而static readonly是在运行 ...

  8. readonly=“readonly”与readonly=“true”

    <input id="u" readonly /> <input id="u" readonly="readonly" / ...

  9. Const(常量)与readonly(只读)的区别

    const与readonly定义的值都不能更改,但它们到底有哪些异同点呢? Const ² Const是常量的意思,其定义的变量只能读取不能更改,且只能在定义时初始化,不能在构造函数与其它属性与方法中 ...

随机推荐

  1. Linux系统下ssh的相关配置详细解析

    Linux系统下ssh的相关配置进行了详细的分析介绍. ssh是大家常用的登录linux服务器的方式,但是为了安全考虑,有时候我们需要针对ssh做一些特殊处理,本文记录笔者曾经做过的一些修改,供大家参 ...

  2. Kakfa揭秘 Day1 Kafka原理内幕

    Spark Streaming揭秘 Day32 Kafka原理内幕 今天开始,会有几天的时间,和大家研究下Kafka.在大数据处理体系中,kafka的重要性不亚于SparkStreaming.可以认为 ...

  3. Python 中的引用和类属性的初步理解

    最近对Python 的对象引用机制稍微研究了一下,留下笔记,以供查阅. 首先有一点是明确的:「Python 中一切皆对象」. 那么,这到底意味着什么呢? 如下代码: #!/usr/bin/env py ...

  4. 两年的坚持,最后还是决定将ISoft开源

    还记得2011年9月份,我在上大四,本来想着考研能上个好点的学校,可我怎么就不愿去自习室上自习.每天晚上睡觉前都告诉自己明天早晨一定早起去上自习,但又每次醒来都不想起床啊,懒,没办法.睡到不想再睡了才 ...

  5. Android Studio 单刷《第一行代码》系列 06 —— Fragment 生命周期

    前情提要(Previously) 本系列将使用 Android Studio 将<第一行代码>(书中讲解案例使用Eclipse)刷一遍,旨在为想入坑 Android 开发,并选择 Andr ...

  6. shell复习---文件解压命令

    需要自己部署服务端,故在申请了空间之后,需要自己安装linux自己,自己安装Apache等,所以下载的压缩文件需要运行.网上找了一些解压命令不对,特别试了下面的方法有效,特别记录一下: 用ssh 登陆 ...

  7. DOCTYPE html PUBLIC 指定了 HTML 文档遵循的文档类型定义

    DOCTYPE html PUBLIC 指定了 HTML 文档遵循的文档类型定义 今天看到一篇CSS应用的一个友好搜索,我按网页上的代码复制.粘贴后预览时总达不到效果,而直接拷贝他的实例却能达到效果, ...

  8. [JavaScript] js验证身份证

    function checkIdCard(){     var vcity={11:"北京",12:"天津",13:"河北",14:&quo ...

  9. suse pshell连接不上

    /etc/ssh/sshd_config #PasswordAuthentication no改成yessuse默认为密钥认证

  10. uva 11151

    求最长回文串  就是将字符串翻转后求最长公子列..... #include <cstdio> #include <cstdlib> #include <algorithm ...