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

Constants and readonly fields in C Sharp (C#) - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - Constants and readonly fields
Declare constant with 'const'
Value fixed at compile time
Use constant anywhere
Declare readonly field with 'readonly'
Value set at declaration or in constructor
Value fixed after construction
Use readonly field anywhere
Constants are fixed values set at compile time. Readonly fields can be set once during object creation and then stay fixed.
Execution Sample
C Sharp (C#)
class Example {
  public const int DaysInWeek = 7;
  public readonly int Year;
  public Example(int year) {
    Year = year;
  }
}
Defines a constant for days in a week and a readonly field for the year set during object creation.
Execution Table
StepActionValue AssignedWhen AssignedCan Change Later?
1Declare const DaysInWeek7Compile timeNo
2Declare readonly Year fieldunassignedN/AYes, once in constructor
3Create Example object with year=2024Year = 2024ConstructorNo
4Try to change DaysInWeekErrorN/ANo
5Try to change Year after constructionErrorN/ANo
💡 Constants fixed at compile time; readonly fields fixed after constructor; attempts to change cause errors.
Variable Tracker
VariableStartAfter DeclarationAfter ConstructorFinal
DaysInWeekN/A777
YearN/Aunassigned20242024
Key Moments - 3 Insights
Why can't I assign a value to a const variable inside a constructor?
Constants are fixed at compile time and must be assigned where declared, not in constructors. See execution_table step 1 vs step 3.
Can I change a readonly field after the object is created?
No, readonly fields can only be assigned once during declaration or in the constructor. After that, they cannot be changed. See execution_table step 5.
Why does trying to change a const or readonly variable cause an error?
Because const values are fixed at compile time and readonly fields are fixed after construction, the compiler prevents changes to keep data safe. See execution_table steps 4 and 5.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table, what is the value of DaysInWeek after declaration?
A0
B7
Cunassigned
DCannot be assigned
💡 Hint
Check execution_table row 1 and variable_tracker for DaysInWeek after declaration.
At which step does the readonly field Year get its value?
AStep 1
BStep 2
CStep 3
DStep 4
💡 Hint
Look at execution_table step 3 where Year is assigned in the constructor.
If you try to change Year after construction, what happens according to the execution_table?
AError occurs
BValue changes successfully
CValue resets to default
DValue becomes null
💡 Hint
See execution_table step 5 about changing Year after construction.
Concept Snapshot
const: fixed value at compile time, assigned where declared
readonly: assigned once at declaration or in constructor
const cannot be changed anywhere
readonly can be set only during construction
both provide safety for fixed data
Full Transcript
This lesson shows how constants and readonly fields work in C#. Constants are values fixed at compile time and must be assigned where declared. Readonly fields can be assigned once either at declaration or inside the constructor. After that, readonly fields cannot be changed. The execution table traces declaring a const DaysInWeek with value 7, declaring a readonly Year field, assigning Year in the constructor, and shows errors if you try to change these values later. The variable tracker shows DaysInWeek is always 7 and Year is assigned 2024 during construction. Key moments clarify why const cannot be assigned in constructors and why readonly fields cannot be changed after construction. The quiz tests understanding of when values are assigned and what happens if you try to change them. Remember, const is for fixed compile-time values, readonly is for values fixed after object creation.