Difference Between Late Binding and Early Binding
- By Gurunatha Dogi in C#
- Aug 24th, 2014
- 40960
- 1
Hey Friends..! Welcome to my another blog post, I'm very grateful for my "onlinebuff" followers for suggesting me "frequently asked questions" via e-mail. In this blog post we will understand "what is the difference between early binding and late binding?". This question again suggested by one of my valuable onlinebuff friend - "Khadak Bist" Technical Consultant from Questpond.
In C# language, C# compiler that is csc.exe compiler or any other compiler which runs on .NET Framework performs binding only when an object is been assigned to a object variable of a specific type.
In C# there are two types of bounding
1. Early Binding
2. Late Binding
What is an Early Binding?
Methods, Functions and properties which are detected and checked during compile time or during compilation is called as early binding.
In Early binding objects are basically a strong type objects or static type objects.
An object is early bounded only when it is been assigned to a specific type (strong type). If something goes wrong while coding or before an execution of an application the visual studio intellisense automatically shows a compile time error with error line number and .NET Intellisense automatically populates declared objects methods and properties on click of the dot operator.
Now in the further upcoming of an article we will see the real-time demonstration of an early binding using an example.
Early Binding using an example
In this demonstration we will see how the methods are checked/detected during compile-time.
class clsTravelAgent { private int _agentcode = 0; private string _agentname = ""; public int AgentCode { set { _agentcode = value; } get { return _agentcode; } } public string AgentName { set { _agentname = value; } get { return _agentname; } } public void AddTravelAgent() { Console.WriteLine("Adding Travel Agent...!"); } public void UpdateTravelAgent() { Console.WriteLine("Updating Travel Agent...!"); } }
As you see from above code that we have created a class called "clsTravelAgent" with some methods and properties. Now we will call this above class in our main program of a console application.
class Program { static void Main(string[] args) { clsTravelAgent objTravel = new clsTravelAgent(); } }
Now the next step we will use that class-object and try to browse the elements in that class.
class Program { static void Main(string[] args) { clsTravelAgent objTravel = new clsTravelAgent(); objTravel.AddTravelAgent(); objTravel.UpdateTravelAgent(); objTravel.DeleteTravelAgent(); } }
If you look at the above code you can see object "objTravel" does not have method called "DeleteTravelAgent" so it should through an error during compilation.
Let's build this solution and see wheather compiler throws an error or not?. Let me tell you that my visual studio already showing red mark over the "DeleteTravelAgent" method saying that method does not exist. Let me show you the snapshot of my code.
Build this solution by pressing F6
Conclusion : So it is been proved that for static types, strong types compiler checks/detects the methods/properties and detection of methods and properties during compile time is also called as early binding.
Advantages of using early binding
The big advantage of using early binding is for performance and ease of development.
Early-bound objects are significantly faster than late-bound objects which makes code easier to read and maintain.
In Early binding Application runs faster , since no boxing or unboxing (type casting) done here
Early binding reduces the number and severity of run-time errors and upfront (before going to runtime) you can trace an error this makes the coding much more easier and error-free.
The visual studio has compile option where you can compile the code easily just you need to press ctrl + F5 to compile the code. This will not only help you to compile the code but also the compiler will be able to spot errors which you missed or something went wrong.
In early binding since the object is of static type or strong type you will be able to browse all the related-type methods and properties. Just you to type a object-name followed by (.) dot operator. The moment you press a "DOT" key the visual studio intellisense will automatically shows methods, functions and properties related to that object class.
Early binding becomes more effective with the use of "var" keyword which was introduced in c# 3.0.
"VAR" keyword is a implicitly way of defining a strong type. "VAR" type is not a actual type but depending on right-hand side value or object type. It gets converted into that strong-type for that object-type or value. Since its gets converted to a static type it automatically becomes early bounded type object.
class clsDummy{ public void Add(){ } public void Delete(){ } } class Program{ public static void Main(){ var obj = new claDummy(); } }
As you see in the above source code that "VAR" keyword (obj) aurtomatically gets converted into the strong-type and we are able to browse all methods inside the class (clsDummy). Hence we can say that with the use of "VAR" keyword we can minimise the burden of type-casting, make our coding simple (easy to understand), reduce late binding passing and pass our code through early bound technique.
What is Late Binding?
Late Binding is just an opp of early binding. In Late binding functions, methods, variables and properties are detected and checked during the run-time.
Methods, properties which bypasses compile-time checking are dynamic types which are checked/detected during runtime. In late binding compiler does not know what kind of object or actual type of an object or which methods/properties an object contains so it bypass the compile-time checking which was handled by run-time.
The most popular example for late binding is the use of dynamic keyword and use of reflection.
Dynamic was introduced in C# 4.0 and introduced to achieve dynamically static object which pass the compile-time detection but on runtime it detects the object actualy type depending on right-hand object-type and convert it to strong-type.
Dynamic Keyword Example
class Program { static void Main(string[] args) { dynamic dyn = 4; Console.WriteLine(dyn.GetType()); } }
As you see from above example of dynamic keyword during compile-time actual object was not detected by the compiler but the actual type was detected on runtime i.e. System.Int32. For more info on dynamic keyword see Click Here
GetType() method was detected on runtime. Kindly run the program to check the output.
Output
System.Int32;
Reflection Example
Reflection is used get the metadata information of an assembly. Metadata information is been invoked during runtime an example is shown below.
using System.Reflection; class Program { static void Main(string[] args) { Assembly myAssembly = Assembly.LoadFile(@"D:\Binding\ConsoleBinding\Binding\bin\Debug\Binding.dll"); Type myType = myAssembly.GetType("Binding.Class1"); object objInstance = Activator.CreateInstance(myType); Console.WriteLine(objInstance.GetType()); } }
As you see from above code the GetType() method/information is been invoked during the runtime.
For more information on reflection see this
Advantages of using late binding
Late binding is very useful to read meta-date information of an assembly or when an actual-type not known.
It also useful when an actual type is not known.
Late binding is more certain to be version-independent, since everything is decided at the run time.
When you create more references in the project the project size increases and it takes longer to compile project so it better to go with the run-time compilation (Optional case).
Finally it is better go through an early binding technique which makes code manageable, readable and maintainable rather than choosing late binding technique.
Personally i always prefer to use early bounding technique to keep my coding life simple.If you are looking to enjoy some challenges then possibly you can use late binding technique.
So friends this is all about early binding and late binding. If you guys have any doubts kindly let me know through your comment.
Kindly if you find this article useful then please share it with your friends on FB, Twitter and Google Plus.
Gurunatha Dogi
Gurunatha Dogi is a software engineer by profession and founder of Onlinebuff.com, Onlinebuff is a tech blog which covers topics on .NET Fundamentals, Csharp, Asp.Net, PHP, MYSQL, SQL Server and lots more..... read more