31

When discussing sealed classes, the term "virtual function table" is mentioned quite frequently. What exactly is this? I read about a method table a while ago (I don't remember the purpose of the purpose of this either) and a google/search on here brings up C++ related results.

Thanks

Shog9
  • 156,901
  • 35
  • 231
  • 235
GurdeepS
  • 65,107
  • 109
  • 251
  • 387

3 Answers3

38

The "virtual function table" or "virtual method table" is a list of method pointers that each class has. It contains pointers to the virtual methods in the class.

Each instance of a class has a pointer to the table, which is used when you call a virtual method from the instance. This is because a call to a virtual method should call the method associated with the class of the actual object, not the class of the reference to the object.

If you for example have an object reference to a string:

object obj = "asdf";

and call the virtual method ToString:

string text = obj.ToString();

it will use the String.ToString method, not the Object.ToString method. It's using the virtual method table of the String class (which the pointer in the string instance is pointing to), not the virtual method table of the Object class.

Guffa
  • 687,336
  • 108
  • 737
  • 1,005
  • 1
    Do I understand correctly that "method table" is created per any type. So that value type has a "method table" as well. And one more question how a type gets aknowledged that it has a "method table"? Is "method table" stored or referenced in a type-object? But value type does not have a type-object pointer. :/ Here is a related question: http://stackoverflow.com/questions/35185528/does-a-value-type-keep-type-pointer-sync-root-static-fields-like-a-reference/35185851#35185851 – Anton Lyhin Feb 03 '16 at 20:51
  • 2
    @Spirit: Yes, every type has a method table (at least in some form at some stage). As calls for value types are non-virtual the exact methods are determined at compile time, so they don't need a virtual method table, for them it would only need to exist in the executable for reflection purposes. I don't know where the method tables are actually stored, that would be implementation dependant so you would go looking in the framework source code for that (however that part of the implementation might not be public). – Guffa Feb 05 '16 at 17:17
  • Thank you. Your answer your is very clear. I have finally found a similar SO discussion: http://stackoverflow.com/questions/926352/how-is-valuetype-gettype-able-to-determine-the-type-of-the-struct – Anton Lyhin Feb 05 '16 at 22:36
  • The IL code for `string text = obj.ToString();` is: `callvirt instance string [mscorlib]System.Object::ToString()`, so it seems it actually doesn't use String.ToString. – OfirD Aug 30 '16 at 15:50
  • 2
    @HeyJude The compile time type of obj is object. Since ToString is virtual and object is a reference type the invocation will happen with the callvirt keyword. Callvirt will call String.ToString() after looking at the virtual method table of the obj instance. It might seem like it wouldn't call String.ToString, but it actually does. – Noel Widmer Jan 24 '17 at 11:43
  • Are non-virtual methods contained in the virtual table or is there a seperated non-virtual method table for them? @Guffa – Claude Tan Aug 24 '17 at 03:34
10

The C# virtual function table works basically the same as the C++ one, so any resources which describe how the C++ virtual function table works should help you pretty well with the C# one as well.

For example, Wikipedia's description is not bad.

Dean Harding
  • 71,468
  • 13
  • 145
  • 180
2

The definition for Virtual Function Table (often call vtable) is the same in C# and C++. It is an lookup table use internally by the runtime to achieve the polymorphic behaviors of abstract/virtual/override method.

In simple you can imagine a base class with a virtual methodA will have a vtable that contains methodA. When derived class override methodA, the vtable for methodA inherits from the parent class but will now points to the methodA in the derived class rather than parent class.

Fadrian Sudaman
  • 6,405
  • 21
  • 29