1. Const and READONLY
Const , whose decorated field can only be initialized when it declares itself.
Readonly is a read-only variable that belongs to a runtime variable and can change its value when the class is initialized. A field of this type that can be initialized in a declaration or constructor.
Therefore, depending on the constructor used, the ReadOnly field may have different values. Const can only be initialized with constants at an early stage. For each compiled result, the const value is fixed, and the value of ReadOnly can be determined at run time.
2. Const and Static
static variables are defined by static. You can change its value externally.
const and static cannot be used together
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 function, and so on. Can be mixed in most cases.
The difference between the two is that the value of theconst is determined during compilation, so its value can only be specified by a constant expression at the time of declaration. Statically , ReadOnly calculates its value at run time, so it can also be assigned by a static constructor . Having understood this essential difference, it is not difficult to see whether static readonly and const are interchangeable in the following statement:
static readonly MyClass myins = new MyClass (); 1static readonly MyClass myins = null; 2static readonly int A = B * 20; 3static readonly int B = 10;static readonly int [] Constintarray = new int[] {1, 2, 3}; 4void SomeFunction ()//5{ 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 looks like the array of three-way is 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:
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; }}
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 = Newmyclass ();
...
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 next two statements will have an error.
Reference Blog
1.HelloLWei. C # static const and readonly differences, 2011-11.
The difference between const, static, and ReadOnly in C #