How Base Calls Work

Now that you know how virtual calls are made, one remaining question is how a base call avoids getting trapped in some kind of endless recursion. Consider the following code we wrote for Student:

class Student : Person{    public override string ToString()    {        return base.ToString() + " and attends " + College;    }}

Assume the base call for ToString would be handled in a virtual manner using a callvirt instruction for System.Object::ToString. The innards of virtual dispatch would inspect the vtable of the runtime type of the object (that is, a Student). In the slot for ToString, a reference would be found to the Student’s ToString code, as shown in the preceding code. So, we end up where ...

Get C# 5.0 Unleashed now with O’Reilly online learning.

O’Reilly members experience live online training, plus books, videos, and digital content from 200+ publishers.