(1) Both readonly and Const are used to mark constants.
(2) Initialization of the assignment is different.
Const-decorated constants must be assigned at the same time as they are declared. For example:
Copy Code code as follows:
public class Class1
{
public const int MaxValue = 10; Correct statement
Public Const MinValue; Error: Constant field requirement provides a value
Public Class1 ()
{
MinValue = 10;
}
}
The readonly field can be assigned a value during initialization (declaration or constructor). Therefore, depending on the constructor used, the ReadOnly field may have different values.
Copy Code code as follows:
public class Class1
{
public readonly int c = 10; Correct statement
public ReadOnly int z;
Public Class1 ()
{
z = 24;//Correct
}
protected void Load ()
{
z = 24;//Error: Cannot assign a value to a read-only field (except in the constructor or variable initial value specified)
}
}
ReadOnly are instance members, so different instances can have different constant values, which is readonly more flexible.
Copy Code code 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 a compile-time constant, and the ReadOnly field can be used to run a constant number of times.
Const requires the compiler to compute a determined value at compile time. At compile time, use the calculated value to replace every place that calls the constant. Therefore, you cannot initialize a constant by extracting a value from a variable.
ReadOnly allows you to set a field to a constant, but you can perform some operations to determine its initial value. Because ReadOnly is performed at calculation time, it can be initialized with some variables. This value is not determined at run time.
(4) The const default is static, and readonly must display the declaration if it is set to static.
(5) The type of a const-decorated value is also restricted, and 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 A reference type that can be declared as const can only be a string or other reference type with a value of NULL. ReadOnly can be of any type.
This means that when we need a const constant, if his type restricts it to be computed when it cannot compile, then we can take it as a static readonly. But there is a slight difference between the two. Look at the following two different files.
File1.cs
Copy Code code as follows:
Using System;
Namespace MyNamespace1
{
public class MyClass1
{
public static readonly int myField = 10;
}
}
File2.cs
Copy Code code as follows:
Namespace MyNamespace2
{
public class MyClass1
{
public static void Main ()
{
Console.WriteLine (MyNamespace1.MyClass1.myField);
}
}
}
Two classes belong to two files File1.cs and File2.cs, and are compiled separately. When the domain MyField declared as static readonly in the file File1.cs, if we change the MyField value to 20 because of a need, So all we need to do is recompile the file File1.cs to File1.dll, which will get 20 when the File2.exe is executed.
However, if you change the static readonly to const and then change the initialization value of the MyField, We have to recompile all the files that are referenced to File1.dll, otherwise our MyNamespace1.MyClass1.myField will not change as we wish. This is particularly noticeable in the larger system development process.
(6) object, array (array), and struct (struct) cannot be declared as const constants.