As we all know, const and static readonly are really like: access through the class name instead of the object name, read-only in the program, and so on. Can be mixed in most cases.
The essential difference between the two is that the value of the const is determined during compilation, so its value can only be specified by a constant expression at the time of declaration. The static readonly is calculated at run time, so you can also assign a value by using a statically constructed function.
Having understood this essential difference, it is not difficult to see whether the static readonly and const in the following statement are interchangeable:
1. static readonly MyClass myins = new MyClass (); 2. static readonly MyClass myins = null;3. static ReadOnly A = B *; static readonly B = 10;4. static readonly int [] Constintarray = new int[] {1, 2, 3};5. void SomeFunction () { const int a = ten; ... }
1: cannot be converted to Const. The new operator is required to execute the constructor, so it cannot be determined during compilation
2: can be converted to Const. We also see that constants of type reference (except string) can only be null.
3: Can be converted to Const. We can say very clearly during compilation that a equals 200.
4: cannot be converted to Const. The truth and 1 are the same, although it seems that the array of the one-way is really a constant.
5: Can not be replaced with readonly,readonly can only be used to modify the class field, can not modify local variables, and can not modify other class members such as property.
Therefore, you can use the static readonly for those places that are inherently constant but cannot be declared with Const.
Examples given in the C # specification include:
1 public class color 2 {3 publicly 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 all public Color (Byte R, Byte G, byte B) { red = r;14 green = g;15 blue = b;16 }17}
One of the problems that static readonly needs to be aware of is that for a static readonly, the reference type is only limited to assignment (write) operations. The reading and writing of its members is still unrestricted.
public static readonly MyClass myins = new MyClass ();
...
Myins. Someproperty = 10; Normal
Myins = new MyClass (); Error, the object is read-only
However, if the MyClass in the example above is not a class but a struct, then the two statements that follow will be faulted.
The difference between Const and static readonly:
Perhaps through the above-mentioned purely conceptual explanation, some beginners somewhat dizzy. Here are some examples to illustrate the following:
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 was {0},b is {1}", A, b); 9 }10}
For the above code, what is the output? A lot of people would think it was a 100,b is 10! In fact, the correct output result is A was 0,b is 10.
Well, if you change to the following: 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 was {0},b is {1}", A, b); 8 } 9 10}
For the above code, how much output is the result? Is it 0,b is 10? In fact, it is wrong, this time the correct output is a was 100,b is
So why is that so? In fact, the above said, const is a static constant, so at compile time will be a and B value down (that is, b variable 10, and a=b*10=10*10=100), then the output of the main function is of course A is 100,b is 10. The static readonly is a dynamic constant, the value of the variable is not parsed during compilation, so the start is the default value, like A and B are int type, ancient capital is 0. And in the execution of the program to A=B*10, so a=0*10=0, the program then executes to b=10 this sentence, only real B's initial value 10 is assigned to B.
"Go" Const and static readonly