比较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. php获取客户端ip地址

    本文介绍一个,php获取客户端的IP地址的实例代码,有需要的朋友参考下吧. 获取客户端IP地址的代码,如下: 复制代码代码示例: <?php//取得客户端IP的函数function get_cl ...

  2. pdo 连接数据库 报错 could not find driver 解决方法

    在windows 下,调试一个PHP程序时,报了这个错误, could not find driver 原来我的这个程序中用到了PDO对象, 连接mysql 5.  在PHP的默认设置中,只打开了ph ...

  3. @Html.TextBox 的使用

    @Html.TextBox( }); //限制 text 的最大输入字符数为 10个 @Html.TextBox("users","",new {@class= ...

  4. C# 学习之旅(2)--- 意外的收获

    今天在完成老师布置的C#作业(计算一元二次方程的根)的时候,收获到意外的知识,所以写此博文予以记录. 意外收获为: 对文本框的输入值进行检测,使之按照要求格式输入. 下面是整个的源代码: using ...

  5. psp系统需求分析

    软件开发方向“PSP系统”软件需求规约 目录 1 引言... 4 1.1 目的... 4 1.2 文档格式... 4 1.3 预期的读者和阅读建议... 4 1.4 范围... 5 1.5 术语... ...

  6. drop column与set unused

    8i以前,如果需要删除表中的列,需要删除表然后重新建.现在,但我们需要删除一个列时,可以有以下两种方法: Logical Delete Physical Delete Logical Delete(逻 ...

  7. Jetty 与 Tomcat 比较,及性能分析

    主流java的web容器,主要是Tomcat, jboss, jetty, resin.由于以前我们主要用的是jboss4.0.5,但jbosse用的servlet容器是tomcat5.5,所以只进行 ...

  8. 浏览器对象模型BOM(Browser Object Model)

    1.结构 BOM是Browser Object Model的缩写,简称浏览器对象模型 BOM提供了独立于内容而与浏览器窗口进行交互的对象 由于BOM主要用于管理窗口与窗口之间的通讯,因此其核心对象是w ...

  9. REST Design Concerns

    Less Requests, More data; one of the core RESTful API design paradigms is the concept of less API re ...

  10. C#查找子串在原串中出现次数

    提供的是一种思路,和具体语言无关. string test = "good good study day day up"; string r = test.Replace(&quo ...