Tuesday 26 September 2017

C# - Early Binding and Late Binding with Example & Difference b/w Early & Late Binding

Early Binding and Late Binding concepts occurred in polymorphism. First we will learn about polymorphism

Polymorphism

Polymorphism means many forms (ability to take more than one form). In Polymorphism poly means “multiple” and morph means “forms” so polymorphism means many forms.


In polymorphism we will declare methods with same name and different parameters in same class or methods with same name and same parameters in different classes. Polymorphism has ability to provide different implementation of methods that are implemented with same name.

In Polymorphism we have 2 different types those are

     -      Compile Time Polymorphism (Called as Early Binding or Overloading or static binding)

     -      Run Time Polymorphism (Called as Late Binding or Overriding or dynamic binding)

Compile Time Polymorphism or Early Binding

Compile time polymorphism means we will declare methods with same name but different signatures because of this we will perform different tasks with same method name. This compile time polymorphism also called as early binding or method overloading.

Method Overloading or compile time polymorphism means same method names with different signatures (different parameters)

For more details check this link polymorphism in c#

Run Time Polymorphism or Late Binding

Run time polymorphism also called as late binding or method overriding or dynamic polymorphism. Run time polymorphism or method overriding means same method names with same signatures.

In this run time polymorphism or method overriding we can override a method in base class by creating similar function in derived class this can be achieved by using inheritance principle and using “virtual & override” keywords.



----------------------

Early Binding

The name itself describes that compiler knows about what kind of object it is, what are all the methods and properties it contains. As soon as you declared the object, .NET Intellisense will populate its methods and properties on click of the dot button.

Common Examples: 
  • ComboBox cboItems;
  • ListBox lstItems;
In the above examples, if we type the cboItem and place a dot followed by, it will automatically populate all the methods, events and properties of a combo box, because compiler already know it's an combobox.

Late Binding

The name itself describes that compiler does not know what kind of object it is, what are all the methods and properties it contains. You have to declare it as an object, later you need get the type of the object, methods that are stored in it. Everything will be known at the run time. 

Common Examples:
  • Object objItems;
  • objItems = CreateObject("DLL or Assembly name");
Here during the compile time, type of objItems is not determined. We are creating an object of a dll and assigning it to the objItems, so everything is determined at the run time. 

Early Binding vs. Late Binding

Now coming into the pictureĆ¢€¦
  • Application will run faster in Early binding, since no boxing or unboxing are done here.
  • Easier to write the code in Early binding, since the intellisense will be automatically populated
  • Minimal Errors in Early binding, since the syntax is checked during the compile time itself.
  • Late binding would support in all kind of versions, since everything is decided at the run time. 
  • Minimal Impact of code in future enhancements, if Late Binding is used.
  • Performance will be code in early binding.

No comments:

Post a Comment

Recent Post

Parallel Task in .Net 4.0