Bird
Raised Fist0
C Sharp (C#)programming~10 mins

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

Choose your learning style10 modes available

Start learning this pattern below

Jump into concepts and practice - no test required

or
Recommended
Test this pattern10 questions across easy, medium, and hard to know if this pattern is strong
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.

Practice

(1/5)
1. What does the base keyword do in C#?
easy
A. It creates a new instance of a class.
B. It allows a child class to access members of its parent class.
C. It defines a new class.
D. It deletes an object from memory.

Solution

  1. Step 1: Understand the role of base

    The base keyword is used in a child class to refer to its parent class members.
  2. Step 2: Identify what base allows

    It allows access to parent class methods, properties, or constructors from the child class.
  3. Final Answer:

    It allows a child class to access members of its parent class. -> Option B
  4. Quick Check:

    base keyword = access parent members [OK]
Hint: Remember: base = parent class access [OK]
Common Mistakes:
  • Confusing base with new instance creation
  • Thinking base deletes objects
  • Assuming base defines a class
2. Which of the following is the correct way to call a parent class constructor using base in C#?
easy
A. public Child() : base() { }
B. public Child() base() { }
C. public Child() call base() { }
D. public Child() : parent() { }

Solution

  1. Step 1: Recall syntax for calling parent constructor

    In C#, to call a parent constructor, use : base() after the child constructor signature.
  2. Step 2: Match correct syntax

    public Child() : base() { } uses public Child() : base() { }, which is the correct syntax.
  3. Final Answer:

    public Child() : base() { } -> Option A
  4. Quick Check:

    Parent constructor call = : base() [OK]
Hint: Use colon and base() after constructor name [OK]
Common Mistakes:
  • Omitting colon before base()
  • Using base() inside constructor body incorrectly
  • Using wrong keyword like parent()
3. What will be the output of the following code?
class Parent {
    public virtual void Show() {
        Console.WriteLine("Parent Show");
    }
}
class Child : Parent {
    public override void Show() {
        base.Show();
        Console.WriteLine("Child Show");
    }
}

var obj = new Child();
obj.Show();
medium
A. Child Show
B. Parent Show
C. Compilation error
D. Parent Show\nChild Show

Solution

  1. Step 1: Understand method overriding and base call

    The Child class overrides Show() and calls base.Show() which runs the Parent version first.
  2. Step 2: Trace the output

    First, "Parent Show" is printed from base.Show(), then "Child Show" is printed from the child method.
  3. Final Answer:

    Parent Show Child Show -> Option D
  4. Quick Check:

    base.Method() runs parent method first [OK]
Hint: base.Method() runs parent method inside override [OK]
Common Mistakes:
  • Ignoring base.Show() call
  • Expecting only child output
  • Thinking code causes error
4. Identify the error in this code snippet:
class Parent {
    public void Display() {
        Console.WriteLine("Parent Display");
    }
}
class Child : Parent {
    public override void Display() {
        base.Display();
        Console.WriteLine("Child Display");
    }
}
medium
A. Cannot override non-virtual method Display() in Parent.
B. base.Display() is invalid syntax.
C. Child class must not call base.Display().
D. No error, code runs fine.

Solution

  1. Step 1: Check method overriding rules

    In C#, only methods marked virtual or abstract in the parent can be overridden.
  2. Step 2: Analyze the Parent class method

    The Display() method in Parent is not virtual, so override in Child causes a compile error.
  3. Final Answer:

    Cannot override non-virtual method Display() in Parent. -> Option A
  4. Quick Check:

    Override requires virtual method [OK]
Hint: Only virtual methods can be overridden [OK]
Common Mistakes:
  • Trying to override non-virtual method
  • Confusing base call syntax
  • Assuming override works without virtual
5. Given the classes below, what will be the output?
class A {
    public virtual string GetName() => "A";
}
class B : A {
    public override string GetName() => "B";
}
class C : B {
    public override string GetName() => base.GetName() + "C";
}

var obj = new C();
Console.WriteLine(obj.GetName());
hard
A. AC
B. Compilation error
C. BC
D. C

Solution

  1. Step 1: Trace method calls through inheritance

    Class C overrides GetName() and calls base.GetName(), which refers to B's override returning "B".
  2. Step 2: Combine returned strings

    C appends "C" to the result from B, so the final string is "B" + "C" = "BC".
  3. Final Answer:

    BC -> Option C
  4. Quick Check:

    base.GetName() calls immediate parent method [OK]
Hint: base calls immediate parent method, not grandparent [OK]
Common Mistakes:
  • Assuming base calls grandparent method
  • Ignoring string concatenation
  • Expecting only "C" output