之前只是知道Const和static readonlyd的区别在于const的值是在编译期间肯定的,而static readonly是在运行时计算出其值的。今天看到Resharper智能提示让用html
static readonly修饰的field改为const修饰,因而忽然想了解一下resharper为何这么提示,因此整理以下:数组
咱们都知道,const和static readonly的确很像:经过类名而不是对象名进行访问,在程序中只读等等。在多数状况下能够混用。
两者本质的区别在于,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等其余类成员。post
所以,对于那些本质上应该是常量,可是却没法使用const来声明的地方,可使用static readonly。 url
例如C#规范中给出的例子:spa
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(); //出错,该对象是只读的code
可是,若是上例中的MyClass不是一个class而是一个struct,那么后面的两个语句就都会出错。htm
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。blog
好吧,若是改为下面的话:
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。