Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do virtual methods work in C#?

Tags:

c#

.net

I know in C++ there are vpointer and vtable. The virtual function table is a list of method pointers to the virtual methods in the class. Each instance of a class has a pointer to the table, which is used when we call a virtual method from the instance.

I want to know how this implemented in C#. As I know the concept of virtual tables are the same. But what about vpointer is GetType() is used instead.

I would appreciate as much details as possible. Thank you.

like image 582
NDeveloper Avatar asked Jul 22 '10 16:07

NDeveloper


1 Answers

Vtables are one possible approach in C++, they are not mandated by the C++ standard. The approach .NET uses clearly meets the published standards for the CLI and C#, but the implementation details are not specified (and could potentially change).

You can infer some details from the way that interfaces, overridden members and "new" members work.

Why do you want to know this? If you have a specific problem then stating the problem will allow others to address that directly.

like image 106
Richard Avatar answered Oct 10 '22 02:10

Richard