SoFunction
Updated on 2025-03-07

Introduction to the usage of const, readonly and static keywords in C#

If there is a value that doesn't change much, we often use const and readonly. What are the differences between these two? Sometimes, we also add the keyword static before readonly, what does this mean?

const

  • Const is static by default and can be accessed through "Class Name. Field Name".
  • The const variable can only be assigned values ​​when declared, and cannot be assigned values ​​to const type variables in the constructor.
  • Once the assembly is compiled, the const variable is written into the assembly's IL code. If you want to modify the value of the const variable, you must regenerate the assembly after modifying the value.
  • const is a compile-time variable
    public class Test
    {
        public const int defaultValue = 10;
        //An error is reported here: because the const variable cannot be assigned to the constructor.        public Test()
        {
            defaultValue = 1000;
        }
    }

above,
You can get the value of the variable defaultValue by.
Assigning a defaultValue value in the Test construct will report an error, and the initial value can only be assigned when the defaultValue is declared.

readonly

readonly is an instance variable by default and can only be accessed through "Object instance. Field Name".
Readonly variables can be assigned values ​​at the time of declaration or within the constructor.
If you want to change the readonly variable value, you only need to modify it within the declared variable or constructor, and you do not need to regenerate the assembly.
readonly is a runtime variable

    class Program
    {
        static void Main(string[] args)
        {
            Test test = new Test();
            ();
            ();
        }
    }
    public class Test
    {
        public readonly int rdValue;
        public Test()
        {
             = 100;
        }
        //An error is reported here: because you can only assign values ​​to readonly variables within declared variables or constructors        public int RDVaue
        {
            get { return rdValue; }
            set { rdValue = value; }
        }
    }

above,
Get the readonly variable value through an instance of the Test class.
Assigning a value to the readonly variable rdValue in the RDValue property will result in an error.

If you prefix static keywords:

At this time, the readonly variable can only be assigned when declaring the variable
At this time, the readonly variable can be accessed through "Class Name. Field Name"
At this time, the readonly variable becomes a compile-time variable

    class Program
    {
        static void Main(string[] args)
        {
            ();
            ();
        }
    }
    public class Test
    {
        public static readonly int rdValue=100;
    }

above,
It can only be assigned when the static readonly variable rdValue is declared.
Access static readonly variables by.

Summary: const is a static, compile-time variable, and can only be assigned when declaring a variable; readonly is a runtime variable, and can be assigned when declaring it or within the constructor. When the keyword static is added before readonly and becomes static readonly, the static readonly variable at this time becomes a static, compile-time variable.

The above is the entire content of this article. I hope that the content of this article has certain reference value for your study or work. Thank you for your support. If you want to know more about it, please see the following links