Skip to main content
added 15 characters in body
Source Link
Jason Baker
  • 196.5k
  • 137
  • 381
  • 520

Variables marked const are little more than strongly typed #define macros, at compile time const variable references are replaced with inline literal values. As a consequence only certain built-in primitive value types can be used in this way. Variables marked readonly can be set, in a constructor, at run-time and their read-only-ness is enforced during run-time as well. There is some minor performance cost associated with this but it means you can use readonly with any type (even reference types).

Also, const variables are inherently static, whereas readonly variables can be instance specific if desired.

Variables marked const are little more than #define macros, at compile time const variable references are replaced with inline literal values. As a consequence only certain built-in primitive value types can be used in this way. Variables marked readonly can be set, in a constructor, at run-time and their read-only-ness is enforced during run-time as well. There is some minor performance cost associated with this but it means you can use readonly with any type (even reference types).

Also, const variables are inherently static, whereas readonly variables can be instance specific if desired.

Variables marked const are little more than strongly typed #define macros, at compile time const variable references are replaced with inline literal values. As a consequence only certain built-in primitive value types can be used in this way. Variables marked readonly can be set, in a constructor, at run-time and their read-only-ness is enforced during run-time as well. There is some minor performance cost associated with this but it means you can use readonly with any type (even reference types).

Also, const variables are inherently static, whereas readonly variables can be instance specific if desired.

Source Link
Wedge
  • 19.7k
  • 7
  • 48
  • 71

Variables marked const are little more than #define macros, at compile time const variable references are replaced with inline literal values. As a consequence only certain built-in primitive value types can be used in this way. Variables marked readonly can be set, in a constructor, at run-time and their read-only-ness is enforced during run-time as well. There is some minor performance cost associated with this but it means you can use readonly with any type (even reference types).

Also, const variables are inherently static, whereas readonly variables can be instance specific if desired.