answersLogoWhite

0

Whats the difference between static and constant?

Updated: 8/16/2019
User Avatar

Wiki User

15y ago

Best Answer

The value of a static readonly field is set at runtime; therefore, the value can be modified by the containing class. On the other hand, the value of a const field is set to a compile-time constant. In the case of static readonly, the containing class is allowed to modify the value only: * in the variable declaration (via a variable initializer) * in the static constructor (or instance constructors for non-static)

Typically, static readonly is used either when the value is unknown at compile time or if the type of the field is not allowed in a const declaration. Also, instance readonly fields are allowed. Note: for reference types, in both cases-static and instance-the readonly modifier only prevents assignment of a new reference to the field. It does not specifically make the object pointed to by the reference immutable.

User Avatar

Wiki User

15y ago
This answer is:
User Avatar

Add your answer:

Earn +20 pts
Q: Whats the difference between static and constant?
Write your answer...
Submit
Still have questions?
magnify glass
imp