Practice mode C#

What is the key difference between const and readonly fields in C#?

Correct answer B. const is fixed at compile time; readonly can be assigned at declaration or in a constructor

Explanation

A const value must be a compile-time constant and is implicitly static, so it is baked into calling code. A readonly field can be given its value in the declaration or in any constructor, so it can depend on run-time data such as DateTime.Now or a constructor argument, and different instances can hold different values. After construction neither can be reassigned.

Report a problem

Answer checked How we write and check questions

Tip: press A, B, C or D to answer, and N for the next question.

Advertisement

Have a question about this answer?

Your email address will not be published. Required fields are marked *

Links and website addresses are not allowed in comments.