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

Virtual method dispatch mechanism in C Sharp (C#) - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - Virtual method dispatch mechanism
Create base class object
Call virtual method
Check actual object type
If base type
Call base method
If derived type
Call overridden method
Return method result
When calling a virtual method, the program checks the actual object's type at runtime and calls the correct method version.
Execution Sample
C Sharp (C#)
class Animal {
  public virtual string Speak() => "Animal sound";
}
class Dog : Animal {
  public override string Speak() => "Bark";
}
Animal a = new Dog();
Console.WriteLine(a.Speak());
This code creates a Dog object but stores it as an Animal reference, then calls Speak(), which runs Dog's version.
Execution Table
StepActionObject TypeMethod CalledOutput
1Create Dog object assigned to Animal referenceDogN/AN/A
2Call Speak() on Animal referenceDogDog.Speak()"Bark"
3Print outputDogN/ABark
4End of executionN/AN/AN/A
💡 Execution stops after printing the overridden method output.
Variable Tracker
VariableStartAfter Step 1After Step 2Final
anullDog object referenceDog object referenceDog object reference
Key Moments - 2 Insights
Why does calling Speak() on an Animal reference run Dog's Speak()?
Because the method is virtual, the program checks the actual object type (Dog) at runtime and calls the overridden method (Dog.Speak()), as shown in execution_table step 2.
What if Speak() was not virtual, which method would run?
If Speak() was not virtual, the method called would be based on the reference type (Animal), so Animal.Speak() would run, not Dog.Speak(). This is different from the virtual dispatch shown in step 2.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table, what method is called at step 2?
ADog.Speak()
BAnimal.Speak()
CObject.ToString()
DBase class constructor
💡 Hint
Check the 'Method Called' column in execution_table row for step 2.
At which step is the Dog object created and assigned?
AStep 2
BStep 1
CStep 3
DStep 4
💡 Hint
Look at the 'Action' column in execution_table for object creation.
If Speak() was not virtual, what would be the output at step 3?
A"Bark"
BCompilation error
C"Animal sound"
DNo output
💡 Hint
Refer to key_moments explanation about non-virtual method calls.
Concept Snapshot
Virtual method dispatch means calling a method on a base class reference runs the overridden method in the actual object's class.
Use 'virtual' keyword in base class and 'override' in derived class.
At runtime, the program checks the real object type to decide which method to run.
This enables flexible and dynamic behavior in object-oriented programming.
Full Transcript
This example shows how virtual method dispatch works in C#. We create a Dog object but store it in an Animal variable. When we call Speak() on the Animal variable, the program checks the actual object type at runtime, which is Dog, and calls Dog's Speak() method. This is because Speak() is marked virtual in Animal and overridden in Dog. The execution table shows each step: creating the Dog object, calling Speak(), and printing the output. Key moments explain why the overridden method runs and what would happen if the method was not virtual. The visual quiz tests understanding of method calls and object creation steps. Virtual dispatch allows programs to decide which method to run based on the real object, not just the reference type.