c. Differences between const and readonly in net

  • 2020-07-21 07:29:25
  • OfStack

(1) Both readonly and const are used to indicate constants.
(2) Different initialization assignments.
const - decorated constants must be assigned at the same time they are declared. Such as:

public class Class1
{
    public const int MaxValue = 10;       // The correct statement 
    public const MInValue;                   // Error: Constant field required 1 A value 
    public Class1()
    {
        MinValue = 10;
    }
}

The readonly field can be assigned during initialization (declaration or constructor). Therefore, the readonly field may have different values depending on the constructor used.

public class Class1
{
    public readonly int c = 10;           // The correct statement 
    public readonly int z;
    public Class1()
    {
        z = 24;// correct 
    }
    protected void Load()
    {
        z = 24;// Error: Cannot assign values to read-only fields (except in constructor or variable initializer) 
    }
}

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) The const field is a compile time, while the readonly field can be used for run time.
const requires the compiler to be able to calculate certain values at compile time. At compile time, replace every place where the constant is called with the calculated value. Therefore, a constant cannot be initialized by extracting a value from a variable.
readonly allows you to set a field to a constant, but you can perform a few operations to determine its initial value. Because readonly is executed at computation time, certain variables can be initialized. The value that is only determined at run time.
(4) const is static by default, while readonly must display the declaration if it is set to static.
(5) The types of values modified by const are also limited to 1 (or can be converted to) of the following types: sbyte, byte, short, ushort, int, uint, long, ulong, char, float, double, decimal, bool, string, enum types or reference types. Note that reference types that can be declared as const can only be string or other reference types with a value of null. readonly can be of any type.
This means that when we need a constant for const, we can declare it as static readonly if its type restricts it from being calculated at compile time. But there is a slight difference. Look at the following two different files.
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 es62EN1.cs and ES64en2.cs and are compiled separately. When the domain myField in file ES66en1.cs is declared as static readonly, if we change the value of myField to 20 for some reason, we just need to recompile the file ES72en1.cs to ES74en1.dll and get 20 when file2.exe is executed.
However, if we change static readonly to const and then change the initial value of myField, we must recompile all files referring to ES83en1.dll, otherwise the reference to ES85en1.MyClass1.myField will not change as we wish. This is especially important in large system development processes.
(6) object, Array (array) and struct (structure) cannot be declared as const constants.

Related articles: