Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why are virtual methods considered early bound?

Tags:

c#

One definition of binding is that it is the act of replacing function names with memory addresses.

a) Thus I assume early binding means function calls are replaced with memory addresses during compilation process, while with late binding this replacement happens during runtime?

b) Why are virtual methods also considered early bound (thus the target method is found at compile time, and code is created that will call this method)? As far as I know, with virtual methods the call to actual method is resolved only during runtime and not compile time?!

thanx


EDIT:

1)

A a=new A();
a.M();

As far as I know, it is not known at compile time where on the heap (thus at which memory address ) will instance a be created during runtime. Now, with early binding the function calls are replaced with memory addresses during compilation process. But how can compiler replace function call with memory address, if it doesn’t know where on the heap will object a be created during runtime ( here I’m assuming the address of method a.M will also be at same memory location as a )?

2)

v-table calls are neither early nor late bound. Instead there's an offset into a table of function pointers. The offset is fixed at compile time, but which table the function pointer is chosen from depends on the runtime type of the object (the object contains a hidden pointer to its v-table), so the final function address is found at runtime.

But assuming the object of type T is created via reflection ( thus app doesn’t even know of existence of type T ), then how can at compile time exist an entry point for that type of object?

like image 798
AspOnMyNet Avatar asked Feb 28 '23 10:02

AspOnMyNet


1 Answers

Late Binding

With late binding all you have is the name of the method. At compile time you have no way of known if the method even exists. This is known as "duck typing" in languages such as Ruby or Python.

Late binding is slow because you have to look up the function by name. It is also dangerous because you are not protected from minor spelling errors.

Prior to version 4, C# has no support for late binding aside from explicitly calling the reflection API.

Early Binding

When using early binding you compile against an actual method. This method may be referred to directly or it may be a slot in a V-table. Either way you are garunteed to not throw a MissingMethod exception.

History

Visual Basic was well known for supporting both early and late binding, but due it its other limitations it was never considered a true dynamic language. At the same time, versions prior to 7 (a.k.a. VB.NET) had very poor support for enforcing early binding, making it hard to call it a static language either.

With .NET 4, both C# and VB can be said to offer most features expected of both static and dynamically typed languages.

At one point Java was mistakenly said to have late binding support when it fact it only had early bound, OOP style V-tables. This has caused quite a bit on confusion over the years.

like image 76
Jonathan Allen Avatar answered Mar 13 '23 04:03

Jonathan Allen