Difference between static, readonly, and constant in C# (part 2)
readonly:
🔸 Declared using the
🔸 Only the class level fields can be readonly. The local variables of methods cannot be readonly.
🔸 Readonly fields can be initialized at declaration or in the constructor. Therefore, readonly variables are used for the run-time constants.
🔸 Readonly variable cannot be modified at run-time. It can only be initialized or changed in the constructor.
🔸 Readonly members can be accessed using object, but not
readonly:
🔸 Declared using the
readonly
keyword.🔸 Only the class level fields can be readonly. The local variables of methods cannot be readonly.
🔸 Readonly fields can be initialized at declaration or in the constructor. Therefore, readonly variables are used for the run-time constants.
🔸 Readonly variable cannot be modified at run-time. It can only be initialized or changed in the constructor.
🔸 Readonly members can be accessed using object, but not
ClassName.ReadOnlyVariableName
.