Recall & Review
beginner
What is a constant in C#?
A constant is a value that is set at compile time and cannot be changed during program execution. It is declared with the
const keyword.Click to reveal answer
beginner
What is a readonly field in C#?
A readonly field is a variable that can only be assigned a value during declaration or inside a constructor. It cannot be changed afterwards.
Click to reveal answer
beginner
When can you assign a value to a readonly field?
You can assign a value to a readonly field either when you declare it or inside the constructor of the class.
Click to reveal answer
beginner
Can a constant be assigned a value at runtime?
No, constants must have their values assigned at compile time and cannot be changed at runtime.
Click to reveal answer
beginner
Which keyword is used to declare a constant in C#?
The keyword
const is used to declare a constant.Click to reveal answer
Which keyword declares a value that cannot change after compilation?
✗ Incorrect
The
const keyword declares a compile-time constant that cannot be changed.When can a readonly field be assigned a value?
✗ Incorrect
Readonly fields can be assigned at declaration or inside the constructor.
Which of these can be changed after the object is created?
✗ Incorrect
Only normal variables can be changed after object creation; const and readonly cannot.
What happens if you try to assign a value to a const after declaration?
✗ Incorrect
Assigning a value to a const after declaration causes a compile-time error.
Which keyword allows assignment only once, but at runtime?
✗ Incorrect
Readonly fields can be assigned once at runtime, typically in the constructor.
Explain the difference between a constant and a readonly field in C#.
Think about when and how the values can be set and changed.
You got /4 concepts.
Describe a situation where you would use a readonly field instead of a constant.
Consider values that depend on input or runtime information.
You got /3 concepts.