Const constant
1. it can be used to modify fields or local variables of a class. the variable modified by const can only be a value type, not a reference type. in fact, it can also modify the reference type, but the reference type can only be null and cannot be instantiated. so it is useless to reference variables like this. for example, const
Int [] array = {1, 2, 3} is incorrect. However, const int [] array = NULL; is correct.
2. assign a value when declaring the value. You cannot change the value later. the variable value it modifies is determined during compilation. const int A = 4; yes. const int B; it is wrong if no value is assigned.
3. It is of the static type by default, so if it modifies the class field, it can only be directly referenced through the class, it cannot be referenced through the class instance. It cannot be used together with static to modify the variable.
Readonly
1. Only the fields of the class can be modified, and local variables cannot be modified.
2. assign values during declaration or assign values through constructors. values cannot be assigned elsewhere.
3. It is a little special when modifying the reference type. The referenced variable it modifies cannot reference other types when referencing a type, but the content in the referenced type can be modified.
For example, readonlyint [] array = {1, 2, 3}; we cannot create an array = new [] {4, 5, 6, 7} elsewhere. This will cause an error, however, we can change the value of array [0] = 100 in this way. This is correct.
Static
1. it can be used to modify class fields, functions, and classes. all functions and fields in the static class must be static. static fields and functions can only be referenced directly by class, but cannot be referenced by class instances.
2.Static ConstructorThe static constructor declaration method is similar to the General constructor. You can only add a static. Static constructor to initialize static variables.
We do not need to call the static constructor by ourselves. It is called by the system by default. It is automatically called before a class is instantiated or a static field in the class is used. However, it is not certain when the class is called.
Therefore, you cannot rely on the static constructor of other classes when assigning values to the static constructor. In addition, the static constructor will not be called after one call.
Static readonly
These two keywords can be used in combination. Most of the time, they have the same effect as const, but there are also some differences.
1. assign a value when const must be declared. Static readonly can be assigned without a value first, and then a value in the static constructor.
2. Const cannot modify the reference type. And static readonly can. For example, static readonly classname name = new classname (); is correct. At this time, it is wrong to use const to modify it.
3. Because the variable value modified by const is determined during compilation and the value of static readonly is determined during runtime, the same expression value may be different.
For example
Public class
{
Const int AA = BB + 10;
Const int BB = 10;
Static readonly int A = B + 10; // no value is assigned to B. The default value is 0. If B is placed before a, 20 is returned.
Static readonly int B = 10;
Public voidprintsom ()
{
Console. writeline (AA); // 20
Console. writeline (bb); // 10
Console. writeline (a); // 10
Console. writeline (B); // 10
}
}