- Fields can be declared using the Readyonly modifier. Its role is similar to declaring a field const and cannot be changed once the value is set.
- The const field can be initialized only in the declaration statement of a field, and the Readyonly field may set its value at any of the following locations
A field declaration statement, like a const;
Any constructor of the class, if it is a static field, initialization must be done in the static constructor.
- The Const field value must be determined at compile time, while the Readyonly field value can be determined at run time. This freedom allows you to set different values in different environments, or in different constructors;
- Unlike the const, const is always static, and for the ReadOnly field, two points are correct.
It can be an instance field, or it can be a static field;
It has a storage location in memory
C#--readlyonly keywords