0
0
C Sharp (C#)programming~5 mins

Constants and readonly fields in C Sharp (C#) - Cheat Sheet & Quick Revision

Choose your learning style9 modes available
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?
Astatic
Bconst
Cvar
Dreadonly
When can a readonly field be assigned a value?
AAt declaration or inside constructor
BOnly inside methods
COnly at declaration
DAnywhere in the class
Which of these can be changed after the object is created?
Aconst field
Bconst and readonly fields
Creadonly field
Dnormal variable
What happens if you try to assign a value to a const after declaration?
ACompile-time error
BIt updates the value
CRuntime error
DWarning but allowed
Which keyword allows assignment only once, but at runtime?
Aconst
Bstatic
Creadonly
Dvolatile
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.