blogspot visit counter

Tuesday 18 June 2013

Late binding and Early binding in c#

Late binding and Early binding in c#
This is very common  question for interview so read carefully before attend the interview.It's related to OOPS concepts .

Early Binding :-

1 - In C#, early binding is a process in which a variable is assigned to a specific type of object during its declaration to create an early-bound object.

2- Early binding is also known as compile time polymorphism, static binding and static typing.

3 - During early binding, the C# compiler performs syntax and type checks to ensure that the correct parameter amount and type are passed to the method or property. 

4 - Non-virtual methods are always early bound  

5 -  Early bound just means the target method is found at compile time, and code is created that will call this. Whether its virtual or not (meaning there's an extra step to find it at call time is irrelevant). If the method doesn't exist the compiler will fail to compile the code.

6 - Example

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


Late Binding - 

1 - Late bound means the target method is looked up at run time. Often the textual name of the method is used to look it up. If the method isn't there, bang. The program will crash or go into some exception handling scheme at run time.

2 -  Late binding is also known as Run  time polymorphism, Dynamic binding .

3 - Calling methods using reflection is an example of late binding. We write the code to achieve this as opposed to the compiler. (E.g. calling COM components.)

4 - Virtual methods are always late bound

5 - By using late binding in C# you cannot determine the error at compile time because compile time does not have any information about assembly type

6 - Example

Object Objitems;

Objitems=CreateObject("Dll Or Assembly Name");
 
Early Binding  VS Late Binding

  • 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

    Related Posts Plugin for WordPress, Blogger...