Q:

What is the difference between late binding and early binding in C#?

0

What is the difference between late binding and early binding in C#?

All Answers

need an explanation for this answer? contact us directly to get an explanation for this answer

Early Binding and Late Binding concepts belong to polymorphism in C#. Polymorphism is the feature of object-oriented programming that allows a language to use the same name in different forms. For example, a method named Add can add integers, doubles, and decimals.
 
Polymorphism we have 2 different types to achieve that:
  • Compile Time also known as Early Binding or Overloading.
  • Run Time is also known as Late Binding or Overriding.

Compile Time Polymorphism or Early Binding

In Compile time polymorphism or Early Binding, we will use multiple methods with the same name but different types of parameters, or maybe the number of parameters. Because of this, we can perform different-different tasks with the same method name in the same class which is also known as Method overloading. Let see an example code,

using System;
public class Addition
{
    public int Add(int a, int b, int c)
    {
        return a + b + c;
    }
    public int Add(int a, int b)
    {
        return a + b;
    }
}
class Program
{
    static void Main(string[] args)
    {
        Addition dataClass = new Addition();
        int add2 = dataClass.Add(45, 34, 67);
        int add1 = dataClass.Add(23, 34);
        Console.WriteLine("Add Results: {0},{1}",add1,add2);
    }
}

Output:

Add Results: 57,146

 

Run Time Polymorphism or Late Binding

Run time polymorphism is also known as late binding. In Run Time Polymorphism or Late Binding, we can use the same method names with the same signatures, which means the same type or the same number of parameters, but not in the same class because the compiler doesn’t allow for that at compile time.

Therefore, we can use that bind at run time in the derived class when a child class or derived class object will be instantiated. That’s why we call it Late Binding.  Let see an example code,

using System;
class UnknownAnimal  // Base class (parent)
{
    public virtual void animalSound()
    {
        Console.WriteLine("Unknown Animal sound");
    }
}
class Dog : UnknownAnimal  // Derived class (child)
{
    public override void animalSound()
    {
        Console.WriteLine("The dog says: bow wow");
    }
}
class Program
{
    static void Main(string[] args)
    {
        // Create a UnknownAnimal object
        UnknownAnimal someAnimal = new UnknownAnimal();
        // Create a Dog object
        UnknownAnimal myDog = new Dog();
        someAnimal.animalSound();
        myDog.animalSound();
    }
}

Output:

Unknown Animal sound
The dog says: bow wow

need an explanation for this answer? contact us directly to get an explanation for this answer

total answers (1)

C# Interview Questions and Answers,You Need To Know

This question belongs to these collections

Similar questions


need a help?


find thousands of online teachers now
What are the differences between IEnumerable and I... >>
<< What are the advantages of partial classes?...