Multiple choice technology programming languages

Difference between const and readonly

  1. a. readonly value can be changed later

  2. b. const items are dealt at compile time however readonly is dealt at runtime

  3. c. const value can be changed later

  4. d. const items are dealt at runtime however readonly is dealt at compile time

Reveal answer Fill a bubble to check yourself
B Correct answer
Explanation

const is a compile-time constant - the value is embedded at compile time and cannot change. readonly is a runtime constant that can be assigned once (at declaration or in a constructor) and then becomes immutable. This is why const can only be used for value types and string literals, while readonly can reference any type.