• Uncategorized

Difference between Early Binding and Late binding

Binding refers the object type definition. You are using Early Binding if the object type is specified when declaring your object.
E.g. 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.
E.g. 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.

Early binding is just simply explicitly invoking a member, as in

string s = "Hello";
s.Trim();  // Early bound call to the Trim() method

There are lots of late binding mechanisms in .NET, including delegates and reflection. For example, you could use

class Test {

   public void Foo( int i ) {
     ...
   }
}
// Code somewhere else
Test t = new Test();
Type ty = t.GetType();
System.Reflection.MethodInfo mi = ty.GetMethod( "Foo" );
mi.Invoke( t, new object[] { 10 } );

or (via a delegate)

public delegate void FooInvoker( int i );
// code somewhere else
Test t = new Test();

FooInvoker fi = (FooInvoker) Delegate.CreateDelegate( typeof( FooInvoker ), t, "Foo" );
fi( 10 ); // although you could use asynch invoke pattern

Remember that these late binding calls can fail because you might misspell the method name, or pass in the wrong parameters. And please note that there are a lot of ways of invoking a member on an object (or a static member of a type), not just the two shown here.

Also note that some people also use the term late binding to describe virtual function resolution at run-time, as opposed to full early binding where a method is invoked without any virtual lookup being required.

Generally (but see below), early binding is done at compile time (with some work at load time to verify):

int a = 1;
string b = a.ToString();

The call to ToString is completely determined by the compile.

Late binding is done dynamically at runtime (usually via Reflection). Overall, it is more a qualitative statement about performance (direct compiled method calls are orders of magnitude faster than calls via reflection), and dynamism (reflection allows decisions to be made

However, there are many intermediate steps and the whole thing is far from binary. Many different methods exist to call methods which give different performance and flexibility tradeoffs. Including

  • non-virtual method calls
  • virtual method calls
  • delegates
  • Expression Trees
  • reflection
  • dynamically created code (emit and CodeDOM)

It is the ability to do things at runtime you would otherwise have to do at coding time by reading the assembly metadata. This includes working with types that do not exist at compile time (or deployment time even).

Consider how to call each public method (which takes no arguments) on an instance of a type that has a particular attribute ("RunThisAttribute"):

public void RunAnnotatedMethods(object target) {
   Type targetType = target.GetType();
   MethodInfo[] methods
     = targetType.GetMethods()
                 .Where(m => m.GetParameters().Length == 0
                             && m.GetCustomAttributes(typeof(RunThisAttribute), false).Length > 0);
   foreach (MethodInfo m in methods) {
     m.Invoke(target, new object[] {});
   }
}

Deepak Kamboj

Deepak Kamboj is a Solution Architect and Technology Enthusiast, located at Redmond, WA, having 14+ years of hands on experience in the IT industry.

You may also like...