Table of contents
1.
Introduction  
2.
Early Binding
3.
Late Binding
4.
Difference between Early and Late Binding
5.
FAQs
5.1.
Is late binding a slow process?
5.2.
What is run-time binding?
5.3.
How is late binding achieved in C#?
5.4.
Why is run-time polymorphism called late binding?
5.5.
What is Reflection in C#?
6.
Conclusion 
Last Updated: Mar 27, 2024
Easy

Early and Late Binding in C#

Author Shivam Verma
2 upvotes
Career growth poll
Do you think IIT Guwahati certified course can help you in your career?

Introduction  

When an object is assigned to a specified kind of object variable, the C# compiler uses the.NET Framework to perform the binding. C# can perform two different types of bindings:

  • Early Binding
  • Late Binding

Let us discuss these two types of bindings in detail to understand them better.

Early Binding

Early binding refers to detecting and verifying methods and properties during the compilation process. The compiler is aware of the kind of object in this case. The compiler throws an exception during compile time if a method or property does not exist or has a data type problem. Early binding improves performance and makes development easier. Because there is no typecast in early binding, the application runs faster. Early binding minimizes the number of run-time errors.

Example:

Here is an example based on Early binding.

using System;
class Ninjas{
    public string name;
    public string language;
    public void Set_value(string name, string language)
    {
        this.name=name;
        this.language=language;
        Console.WriteLine("My name is: "+name);
        Console.WriteLine("My Favorite Programming language is: "+language);
    }
}
class Program
{
    static void Main(string[] args)
    {
        Ninjas obj=new Ninjas();
        obj.Set_value("Alok", "C#");
        obj.Display();
    }
}

Output

prog.cs(19,13): error CS1061: Type `Ninjas' does not contain a definition for `Display' and no extension method `Display' of type `Ninjas' could be found. Are you missing an assembly reference?
prog.cs(2,7): (Location of the symbol related to previous error)
Compilation failed: 1 error(s), 0 warnings

Here from the above code, we can see that there is no "Display" method written in the class code. Hence the compiler throws an error at compilation time.

Late Binding

Late binding occurs when an object is dynamic or unknown, which will only bind during run-time. Early binding is the polar opposite of late binding. In late binding compilation, the compiler does not know what kind of object it is and what the methods or properties it holds. During run-time, the object type is determined by the data it has on the right-hand side. Late binding is achieved mostly through the use of virtual methods. Because late binding requires run-time lookups, it performs slower than early binding.

Example:

Here is an example based on Late binding.

using System;
class Ninjas
{
    static void Main(string[] args)
    {
        dynamic Dyn_obj1=4;
        dynamic Dyn_obj2=5.678;
        dynamic Dyn_obj3="Ninjas";
        Console.WriteLine("The type of the above dynamic objects:");
        Console.WriteLine(Dyn_obj1.GetType());
        Console.WriteLine(Dyn_obj2.GetType());
        Console.WriteLine(Dyn_obj3.GetType());
    }
}

Output

The type of the above dynamic objects:
System.Int32
System.Double
System.String

In the above code, the Dyn_obj1 has integer type data, Dyn_obj2 has double type data, and Dyn_obj3 has string type data. However, the compiler does not resolve these problems during compilation. These dynamic objects are identified and converted into System.Int32, System.Double and System.String respectively at run-time.

Must Read IEnumerable vs IQueryable, singleton design pattern in c#

Difference between Early and Late Binding

Check out this article - Compile Time Polymorphism

FAQs

Is late binding a slow process?

Late binding is slower than early binding because it needs run-time lookups.

What is run-time binding?

Dynamic or late binding are synonyms for run-time binding. Dynamic binding simply means that the method implementation that is called is determined at run-time rather than at compile time.

How is late binding achieved in C#?

Late binding or run-time binding in C# is achieved with reflection.

Why is run-time polymorphism called late binding?

Run-time polymorphism is also known as dynamic polymorphism or late binding. In run-time polymorphism, the function call is resolved at run time. 

What is Reflection in C#?

The process of reflecting the metadata of types, methods, and fields in a code is called reflection. The namespace System.Reflection allows you to get data about loaded assemblies and their elements, such as classes, methods, and value types.

Must Read Dynamic Binding in C++

Conclusion 

In this article, we have extensively discussed the Early and Late binding in C# progarmming language. We also discuss the difference between Early and Late binding.

We hope that this article has helped you enhance your knowledge regarding the Early and Late Binding in C# and if you would like to learn more, check out our article on the Singleton Design Pattern C#, Differences Between C++ and C# and, C# interview questions for 5 years Experience.

Refer to our guided paths on the Coding Ninjas Studio platform to learn more about DSA, DBMS, Competitive Programming, Python, Java, JavaScript, etc.

Refer to the links problemstop 100 SQL problemsresources, and mock tests to enhance your knowledge.

Do upvote our blog to help other ninjas grow. Happy Coding!

Live masterclass