const int a

  • must be initialized
  • initialization must be at compile time

readonly int a

  • can use default value, without initializing
  • initialization can be at run time

二者本质的区别在于,const的值是在编译期间确定的,因此只能在声明时通过常量表达式指定其值。而static readonly是在运行时计算出其值的,所以还可以通过静态构造函数来赋值。 明白了这个本质区别,我们就不难看出下面的语句中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等其他类成员。

因此,对于那些本质上应该是常量,但是却无法使用const来声明的地方,可以使用static readonly。

例如C#规范中给出的例子:

 1 public class Color
2 {
3 public static readonly Color Black = new Color(0, 0, 0);
4 public static readonly Color White = new Color(255, 255, 255);
5 public static readonly Color Red = new Color(255, 0, 0);
6 public static readonly Color Green = new Color(0, 255, 0);
7 public static readonly Color Blue = new Color(0, 0, 255);
8
9 private byte red, green, blue;
10
11 public Color(byte r, byte g, byte b)
12 {
13 red = r;
14 green = g;
15 blue = b;
16 }
17 }

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

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

Const 和 static readonly的区别:
      可能通过上述纯概念性的讲解,对有些初学者有些晕乎。下面就一些例子来说明下:

 1 using System;
2 class P
3 {
4 static readonly int A=B*10;
5 static readonly int B=10;
6 public static void Main(string[] args)
7 {
8 Console.WriteLine("A is {0},B is {1} ",A,B);
9 }
10 }

对于上述代码,输出结果是多少?很多人会认为是A is 100,B is 10吧!其实,正确的输出结果是A is 0,B is 10。

好吧,如果改成下面的话:using System;

 1 class P
2 {
3 const int A=B*10;
4 const int B=10;
5 public static void Main(string[] args)
6 {
7 Console.WriteLine("A is {0},B is {1} ",A,B);
8 }
9
10 }

对于上述代码,输出结果又是多少呢?难道是A is 0,B is 10?其实又错了,这次正确的输出结果是A is 100,B is
那么为什么是这样的呢?其实在上面说了,const是静态常量,所以在编译的时候就将A与B的值确定下来了(即B变量时10,而A=B*10=10*10=100),那么Main函数中的输出当然是A is 100,B is 10啦。而static readonly则是动态常量,变量的值在编译期间不予以解析,所以开始都是默认值,像A与B都是int类型,故都是0。而在程序执行到A=B*10;所以A=0*10=0,程序接着执行到B=10这句时候,才会真正的B的初值10赋给B。

(C#) What is the difference between "const" and "static readonly" ?的更多相关文章

  1. 到底是 const 还是 static readonly

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

  2. 【转】const和static readonly

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

  3. const 还是 static readonly

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

  4. const和static readonly的区别

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

  5. C# const和static readonly区别

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

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

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

  7. const和static readonly 区别

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

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

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

  9. static, readonly, const

    static Use the static modifier to declare a static member, which belongs to the type itself rather t ...

随机推荐

  1. Fresco最最最简单使用

    http://blog.csdn.net/wa991830558/article/details/46005063

  2. DataTransfer(setData()方法)

    DataTransfer对象专门用来存储拖放时要携带的数据,它可以被设置为拖放事件对象的DataTransfer属性.---把拖动的数据存入其中setData有两个参数:1.第一个参数为携带数据的数据 ...

  3. linux中socket的理解---4

    一.socket 一般来说socket有一个别名也叫做套接字. socket起源于Unix,都可以用“打 开open –> 读写write/read –> 关闭close”模式来操作.So ...

  4. .equal与==的区别

    ==比较的是对象的地址,也就是是否是同一个对象: equal比较的是对象的值. Integer r1 = new Integer(900);//定义r1整型对象Integer r2 = new Int ...

  5. 网站后台的lnmp启动与重启

    网站建立时间很长了,经常挂掉,又没有其他技术人员带.只好自己摸索着修复. 到今天网站已经挂掉了一个礼拜.请求各路大神无果后决定自己修复. 首先出现的是502,网关错误. 1.上阿里云服务用户中心重新启 ...

  6. POJ-1741 Tree (树上点分治)

    题目大意:一棵带边权无根树,边权代表距离,求距离小于等于k的点对儿数. 题目分析:这两个点之间的路径只有两种可能,要么经过根节点,要么在一棵子树内.定义depth(i)表示点 i 到根节点的距离,be ...

  7. Unity Shader _Time

    _Time是个4维向量,跟Unity3D中的deltaTime(这是个一维的,数值)不同. float4 _Time : Time (t/20, t, t*2, t*3), use to animat ...

  8. Dijkstra + 优先队列优化 模板

    #include <cstdio> #include <cstring> #include <queue> #include <vector> #inc ...

  9. c++特性:指向类成员的指针和非类型类模板参数和函数指针返回值 参数推导机制和关联型别

    一.c++允许定义指向类成员的指针,包括类函数成员指针和类数据成员指针 格式如下: class A { public: void func(){printf("This is a funct ...

  10. C函数之memcpy()函数用法

    函数原型 void *memcpy(void*dest, const void *src, size_t n); 功能 由src指向地址为起始地址的连续n个字节的数据复制到以destin指向地址为起始 ...