C# Lessons

Const vs readonly #L4

Today, we will describe very shortly what are the differences between const and read only – used to declare immutable variables.

Const

  • The value of the constant is assigned during compilation. In binary code, specific values appear instead of a variable.
  • The value can only be assigned during the declaration.
  • The const values can’t be marked with the static keyword, because the const is itself somehow static.

Readonly

  • The readonly value can be set both during the declaration and during the run time of the program when creating a new object using the constructor.
  • The readonly can be used with a static modifier.

That’s it! I hope that now you know which one of them you should use in your code. 🙂

Leave a Reply