What is late binding and early binding? What is the benefits and disadvantages of using them?

Showing Answers 1 - 15 of 15 Answers

SoCalBruce

  • Dec 3rd, 2007
 

Binding refers the object type definition. You are using Early Binding if the object type is specified when declaring your object. Eg if you say Dim myObject as ADODB.Recordset that is early binding (VB example).

If on the other hand you declare your object as a generic type you are late binding. Eg if you Dim myObject as Object (VB example).

One should note that binding is determined at object declaration and not at object instantiation. Therefore it does not matter if you later go on to Set myObject = New ADODB.Recordset or Set myObject = CreateObject(”ADODB.Recordset”).

Early binding allows developers to interact with the object’s properties and methods during coding. You can enjoy the benefits of intellisense. Also, early binding permits the compiler to check your code. Errors are caught at compile time. Early binding also results in faster code. It’s disadvantage is that you cannot create a generic object which may be bound to different types of objects.


Late binding on the other hand permits defining generic objects which may be bound to different objects. Eg you could declare myControl as Control without knowing which control you will encounter. You could then query the Controls collection and determine which control you are working on using the TypeOf method and branch to the section of your code that provides for that type. This is the only benefit of late binding. It’s disadvantages are that it is error prone and you will not enjoy much intellisense whilst coding.


PeterVH

  • Aug 19th, 2011
 

If you have a "virtual" method, the compiler isn't going to be absolutely sure if it will be this code being called, unless at runtime. So it kind of postpones the decision making about what to do in case this method is going to get called through potentially derived types.

A second way is generics where you can have a Type T and demand that it implements an interface. All the compiler knows is that it will have to call a certain method (the one from the interface) but not which specific piece of code.

Another way to get late binding is through reflection. Just happened to have used this few days ago, and whipped up a nice example imho

Code
  1. namespace WindowsFormsApplication4

  2. {

  3.     public partial class Form4 : Form

  4.     {

  5.         public Form4() { InitializeComponent(); }

  6.  

  7.         private void button1_Click(object sender, EventArgs e)

  8.         {

  9.             foreach (IExample example in (from x in AppDomain.CurrentDomain.GetAssemblies()

  10.                                           where x.FullName.Contains("WindowsFormsApplication4")

  11.                                           from y in x.GetTypes()

  12.                                           where typeof(IExample).IsAssignableFrom(y) && !y.IsAbstract

  13.                                           select x.CreateInstance(y.FullName)))

  14.             {

  15.                 example.DoSomeStuff();

  16.             }

  17.           /* Debug output =

  18.            * Singing a song

  19.            * Sleeping

  20.            * ... etc */

  21.         }

  22.     }

  23.     public interface IExample

  24.     {

  25.         void DoSomeStuff();

  26.     }

  27.     public class FirstExample : IExample

  28.     {

  29.         public void DoSomeStuff() { Debug.WriteLine("Singing a song"); }

  30.     }

  31.     public class SecondExample : IExample

  32.     {

  33.         public void DoSomeStuff() { Debug.WriteLine("Sleeping"); }

  34.     }

  35.     public class EtcExample : IExample

  36.     {

  37.         public void DoSomeStuff() { Debug.WriteLine("... etc"); }

  38.     }

  39. }

  Was this answer useful?  Yes

  • Aug 22nd, 2011
 

binding is mapping of object variable to the object.
Example
you write Class1 obj=new Class1();
here we say obj is variable of type Class1.
so obj variable is bind to class1 object.
So in the above case
This mapping is actually decided by compiler at compile time or design time.
So this is early binding.
Now when obj does not know at compile time to which object its going to map then its a late binding
Example in remoting
object o=Activator.GetType(typeof(class1),"www.abc.com");
here o does not know to which object it will point unless and until the program is running.

  Was this answer useful?  Yes

Suresh Jayaraman

  • Sep 30th, 2011
 

The polymorphism is achieved by early binding and late binding

Early Binding:

Compiler bind the objects to methods at the compile time.This is called early binding or static binding.Function overloading is example for early binding.

Late Binding:

Compiler bind the objects to methods at the runtime.This is called late binding or dynamic binding.Function overriding is example for late binding.

  Was this answer useful?  Yes

Give your answer:

If you think the above answer is not correct, Please select a reason and add your answer below.

 

Related Answered Questions

 

Related Open Questions