(1) Both readonly and const are used to mark constants.
(2) Different values are assigned during initialization.
The constant modified by const must be assigned a value at the same time as the declaration. For example:
Copy codeThe Code is as follows:
Public class Class1
{
Public const int MaxValue = 10; // correct declaration
Public const MInValue; // error: a constant field requires a value.
Public Class1 ()
{
MinValue = 10;
}
}
The readonly field can be assigned a value during initialization (declaration or constructor. Therefore, the readonly field may have different values based on the constructors used.
Copy codeThe Code is as follows:
Public class Class1
{
Public readonly int c = 10; // correct declaration
Public readonly int z;
Public Class1 ()
{
Z = 24; // correct
}
Protected void Load ()
{
Z = 24; // error: the value of the read-only field cannot be assigned (except for the specified item of the constructor or variable initial value)
}
}
Readonly is an instance Member, so different instances can have different constant values. This is readonly more flexible.
Copy codeThe Code is as follows:
Public readonly Color Red = new Color (255, 0, 0 );
Public readonly Color Green = new Color (0,255, 0 );
Public readonly Color Blue = new Color (0, 0,255 );
(3) The const field is the compile time, while the readonly field can be used to run the time.
Const requires the compiler to be able to calculate a definite value during compilation. During compilation, the calculated value is used to replace every place that calls the constant. Therefore, you cannot extract values from a variable to initialize constants.
Readonly allows a field to be set as a constant, but some operations can be performed to determine its initial value. Because readonly is executed during computing, some variables can be used for initialization. This value is determined at runtime.
(4) const is static by default, and readonly must display the declaration if it is set to static.
(5) The type of the value modified by const is also limited. It can only be one of the following types (or can be converted to the following types ): sbyte, byte, short, ushort, int, uint, long, ulong, char, float, double, decimal, bool, string, enum type or reference type. Note that the reference type declared as const can only be string or other reference types with a null value. Readonly can be of any type.
That is to say, when we need a const constant, if its type limits it, it cannot be computed at the time of compilation, we can solve this problem by interpreting the voice as static readonly. However, there is a slight difference between the two. See the following two different files.
File1.cs
Copy codeThe Code is as follows:
Using System;
Namespace MyNamespace1
{
Public class MyClass1
{
Public static readonly int myField = 10;
}
}
File2.cs
Copy codeThe Code is as follows:
Namespace MyNamespace2
{
Public class MyClass1
{
Public static void Main ()
{
Console. WriteLine (MyNamespace1.MyClass1. myField );
}
}
}
The two classes belong to two files, file1.cs and file2.cs, and are compiled separately. When the field myField in the file file1.cs is declared as static bytes, 20 is returned.
However, if we change static readonly to const and then change the initialization value of myField, we must re-compile all files referenced in file1.dll; otherwise, we will reference MyNamespace1.MyClass1. myField will not change as we wish. This requires special attention in the Process of Large system development.
(6) objects, arrays, and struct cannot be declared as const constants.