转:

Const 定义的是静态常在对象初始化的时候赋值.以后不能改变它的值.属于编译时常量。不能用new初始化。

Readonly 是只读变量.属于运行时变量.可以在类constructor里改变它的值.不能作用于局部变量。

const 和 static 不能在一起用,它已经是静态的了。

我们都知道,const和static readonly的确非常像:通过类名而不是对象名进行访问,在程式中只读等等。在多数情况下能混用。
二者本质的差别在于,const的值是在编译期间确定的,因此只能在声明时通过常量表达式指定其值

而static readonly,在程式中只读, 不过它是在运行时计算出其值的,所以还能通过静态构造函数来对它赋值,

readonly只能用来修饰类的field,不能修饰局部变量,也不能修饰property等其他类成员.

明白了这个本质差别,我们就不难看出下面的语句中static readonly和const能否互换了:
1. static readonly MyClass myins = new MyClass();
2. static readonly MyClass myins = null;
3. static readonly A = B * 20;
   static readonly B = 10;
4. static readonly int [] constIntArray = new int[] {1, 2, 3};
5. void SomeFunction()
   {
      const int a = 10;
      ...
   }
1:不能换成const。new操作符是需要执行构造函数的,所以无法在编译期间确定
2:能换成const。我们也看到,Reference类型的常量(除了String)只能是Null。
3:能换成const。我们能在编译期间非常明确的说,A等于200。
4:不能换成const。道理和1是相同的,虽然看起来1,2,3的数组的确就是个常量。
5:不能换成readonly,readonly只能用来修饰类的field,不能修饰局部变量,也不能修饰property等其他类成员。

public class TestReadonly

{
    private int readonly int x; //X是只读字段
    public TestReadonly()
    {
        // 只能在初始化时,对只读字段赋值
        x = 100;
    }
     
    pubilc int GetX()
    {
        //这个语句是错误的,因为x不能被再次赋值,x是只读的(readonly),而
        // 下面的语句试图改变x的值。
        //x = x +100;
       
         
        //这个语句是正确的,因为语句执行后,x的值没有变
        int x1 = x + 100;
        return x1;
    }
}

因此,对于那些本质上应该是常量,不过却无法使用const来声明的地方,能使用static readonly。例如C#规范中给出的例子:

public class Color
{
    public static readonly Color Black = new Color(0, 0, 0);
    public static readonly Color White = new Color(255, 255, 255);
    public static readonly Color Red = new Color(255, 0, 0);
    public static readonly Color Green = new Color(0, 255, 0);
    public static readonly Color Blue = new Color(0, 0, 255);

private byte red, green, blue;

public Color(byte r, byte g, byte b)
    {
        red = r;
        green = g;
        blue = b;
    }
}

static readonly需要注意的一个问题是,对于一个static readonly的Reference类型,只是被限定不能进行赋值(写)操作而已。而对其成员的读写仍然是不受限制的。
public static readonly MyClass myins = new MyClass();

myins.SomeProperty = 10; //正常
myins = new MyClass();    //出错,该对象是只读的

不过,如果上例中的MyClass不是个class而是个struct,那么后面的两个语句就都会出错。

public static class test
{
public static readonly int ggg = 3;
static test() //public static ...报错 static构造函数不允许出现修饰符
{
ggg = 777;
}

}
public partial class MainWindow : Window
{
static readonly int g = 3;
const int k = 2;
readonly int i = 10;
public MainWindow()
{
// k = 88;//报错 值不可改变 编译时常亮 是静态常量 不能与static组合 MainWindow.k函数外可以这样访问
i = 999;
// g = 777;//报错 g为static修饰 是静态常量 不能再运行改变其值
InitializeComponent();
}

private void Button_Click(object sender, RoutedEventArgs e)
{
MessageBox.Show("hello world !"+" i="+i+" ggg= "+ test.ggg);//i=99 g=777

Window1 window = new Window1();

this.Close();
window.Show();
}

}

C# const, readonly, static readonly的更多相关文章

  1. 到底是 const 还是 static readonly

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

  2. 【转】const和static readonly

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

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

  4. const 还是 static readonly

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

  5. const和static readonly的区别

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

  6. C# const和static readonly区别

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

  7. Unity C# const与static readonly的区别与联系

    using System; namespace Test { class MainClass { //懒人写法的单例 class Weapon { public static readonly Wea ...

  8. const和static readonly 区别

    const的值是在编译期间确定的,因此只能在声明时通过常量表达式指定其值. 而static readonly是在运行时计算出其值的,所以还可以通过静态构造函数来赋值. static readonly ...

  9. c#中const、static、readonly的区别

    1. const与readonly const ,其修饰的字段只能在自身声明时初始化. Readonly 是只读变量,属于运行时变量,可以在类初始化的时候改变它的值.该类型的字段,可以在声明或构造函数 ...

  10. 【Unity|C#】基础篇(6)——const、readonly、static readonly

    [学习资料] <C#图解教程>(第6章):https://www.cnblogs.com/moonache/p/7687551.html 电子书下载:https://pan.baidu.c ...

随机推荐

  1. C# System.Threading.Timer 详解及示例

    前言 定时器功能在日常开发中也是比较常用的,在 .Net 中实际上总共有五种定时器,分别是:System.Timers.Timer.System.Threading.Timer.System.Wind ...

  2. [USACO06NOV] Round Numbers S

    题目 \(\texttt{[USACO06NOV] Round Numbers S}\) 分析 数位 \(dp\) 入门题 一般我们需要当前位置 \(pos\),有无前导零 \(lead\),高位标记 ...

  3. JZOJ 4895【NOIP2016提高A组集训第16场11.15】三部曲

    题目 对于 \(50%\) 的数据,\(1<=n<=1000,1<=p<=300\) 对于 \(100%\) 的数据,\(1<=n<=50000,1<=p&l ...

  4. jquery获得标签的值或元素的内容

    例如: .html() 获取a标签中的i元素 console.error($("a[name=" + index + "]").html()); 设置a标签里的 ...

  5. [WPF]MVVM的数据绑定

    啥是MVVM? 我理解的MVVM是Model(数据),View(界面),ViewModel(数据与界面之间的桥梁)的缩写,是一种编程模式.前期需要多花一些时间去编辑绑定,在后期维护方便.只需要关注数据 ...

  6. 设备树编译链接报错arch/arm/boot/dts/imx50.dtsi:16:42: fatal error: dt-bindings/

    1.vim scripts/Makefile.lib, add 3 lines into dtc_cpp_flags dtc_cpp_flags  = -Wp,-MD,$(depfile).pre.t ...

  7. 06 RDD编程

    总共有多少学生?map(), distinct(), count() 开设了多少门课程? 每个学生选修了多少门课?map(), countByKey() 每门课程有多少个学生选?map(), coun ...

  8. SpringMVC学习笔记【狂神说】

    1.MVC是什么 MVC是模型(Model).视图(View).控制器(Controller)的简写,是一种软件设计规范. 是将业务逻辑.数据.显示分离的方法来组织代码. MVC主要作用是降低了视图与 ...

  9. 应用kafka的经验

    Kafka 部署注意事项? 启动用户,非root 安装目录权限:除了数据目录和日志目录是读写外,bin目录是可执行,其他目录应该只读 默认端口修改 只容许内网访问 集成监控和管理软件 开启认证 Kaf ...

  10. Typora的一些基础用法

    Typora的简单实用技巧 标题 标题分为h1~h6六个等级,数字越小标题越大. 定义标题有一下几种方式. 方式一:这个标题手敲就在文本前边敲#号,#和文本中间需又空格隔开. 方式二:快捷键,Ctrl ...