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

Base keyword behavior in C Sharp (C#) - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - Base keyword behavior
Derived class method called
Use 'base' keyword
Call base class method
Execute base class method code
Return to derived method
Finish derived method
When a derived class method uses 'base', it calls the base class method, runs its code, then returns to finish the derived method.
Execution Sample
C Sharp (C#)
class Base {
  public virtual void Show() {
    Console.WriteLine("Base Show");
  }
}
class Derived : Base {
  public override void Show() {
    base.Show();
    Console.WriteLine("Derived Show");
  }
}
Derived d = new Derived();
d.Show();
This code calls the derived class Show method, which calls the base class Show method first, then prints its own message.
Execution Table
StepMethod CalledActionOutput
1Derived.Show()Starts Derived.Show method
2Derived.Show()Calls base.Show() using 'base' keyword
3Base.Show()Executes Base.Show methodBase Show
4Derived.Show()Returns from base.Show(), continues Derived.Show
5Derived.Show()Prints Derived Show messageDerived Show
6Derived.Show()Method ends
💡 Derived.Show() finishes after calling base.Show() and printing its own message.
Variable Tracker
VariableStartAfter Step 1After Step 2After Step 3After Step 4After Step 5Final
Method Call StackEmptyDerived.Show()base.Show() calledInside Base.Show()Returned to Derived.Show()Printed Derived ShowEmpty
Key Moments - 2 Insights
Why does the base class method run before the derived class prints its message?
Because the derived method explicitly calls base.Show() at step 2, so the base method runs first (steps 3-4) before continuing.
What happens if we remove 'base.Show()' call in the derived method?
The base class method won't run, so only 'Derived Show' prints. The execution_table would skip steps 2 and 3.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution table, what is printed at step 3?
ADerived Show
BBase Show
CNothing
DError
💡 Hint
Check the Output column at step 3 in the execution_table.
At which step does the derived method resume after calling the base method?
AStep 2
BStep 3
CStep 4
DStep 5
💡 Hint
Look for the step where the action says 'Returned to Derived.Show()' in the execution_table.
If 'base.Show()' is removed, which steps disappear from the execution_table?
ASteps 2 and 3
BSteps 1 and 6
CSteps 4 and 5
DNo steps disappear
💡 Hint
Check which steps involve calling and executing base.Show() in the execution_table.
Concept Snapshot
Base keyword calls base class method from derived class.
Use 'base.MethodName()' inside derived method.
Base method runs first, then derived continues.
Without 'base', base method is skipped.
Useful to extend or reuse base behavior.
Full Transcript
This visual trace shows how the 'base' keyword works in C#. When a derived class method calls 'base.Method()', it runs the base class method code first, then returns to finish the derived method. The execution table tracks each step: starting the derived method, calling the base method, executing base code, returning, and finishing derived code. Variables like the call stack show method calls and returns. Key moments clarify why base runs first and what happens if 'base' call is removed. The quiz tests understanding of output and flow. The snapshot summarizes the base keyword usage simply.