(1) readonly and const are both used to mark constants.
(2) The initialization assignment is different.
const-modified constants must be assigned a value at the same time they are declared. For example:
public class Class1 { public const int MaxValue = 10; //正确声明 public const MInValue; //错误:常量字段要求提供一个值 public Class1() { MinValue = 10; } }
readonly fields can be assigned during initialization (declaration or constructor). Therefore, readonly fields may have different values depending on the constructor used.
public class Class1 { public readonly int c = 10; //正确声明 public readonly int z; public Class1() { z = 24;//正确 } protected void Load() { z = 24;//错误:无法对只读的字段赋值(构造函数或变量初始值指定项中除外) } }
readonly is an instance member, so different instances can have different constant values, which makes readonly more flexible.
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) const fields are compile-time constants, while readonly fields can be used for run-time constants.
const requires the compiler to be able to calculate a certain value at compile time. At compile time, every place where the constant is called is replaced with the calculated value. Therefore you cannot extract a value from a variable to initialize a constant.
readonly allows a field to be set to a constant, but some operations can be performed and its initial value can be determined. Because readonly is executed at calculation time, it can be initialized with certain variables. This value is determined at runtime.
(4) const is static by default, and readonly must be explicitly declared if it is set to static.
(5) The type of 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 that can be declared as const can only be string or other reference types whose value is null. readonly can be of any type.
That is to say, when we need a const constant, if its type restricts it from being calculated to a definite value during compilation, then we can solve it by declaring it as static readonly. But there is still a slight difference between the two. Look at the two different documents below.
file1.cs
using System; namespace MyNamespace1 { public class MyClass1 { public static readonly int myField = 10; } }
file2.cs
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 domain myField in the file file1.cs is declared as static readonly, if we change the value of myField to 20 due to some need, then we only need to recompile the file file1.cs to file1.dll. When executing file2.exe That is, you will get 20.
But if you change static readonly to const and then change the initialization value of myField, we must recompile all files that reference file1.dll, otherwise the MyNamespace1.MyClass1.myField we reference will not be as we expected. Willing to change. This is especially important to pay attention to during the development of large systems.
(6) object, Array (array) and struct (structure) cannot be declared as const constants.
The above is the difference between const and readonly in c#.net. For more related content, please pay attention to the PHP Chinese website (www.php.cn)!