Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
What is late binding in C#?
Late binding in C# refers to the process where the decision of which method to call is made at runtime rather than compile time. This is also known as dynamic polymorphism, where the actual method implementation is determined by the object type at runtime, not by the reference type.
Late binding is implemented using virtual methods in base classes and override methods in derived classes. When a virtual method is called through a base class reference, the runtime determines which derived class method to execute based on the actual object type.
How Late Binding Works
In late binding, the method call resolution happens during program execution. The runtime examines the actual object type and calls the appropriate overridden method, even when accessed through a base class reference.
Syntax
Following is the syntax for implementing late binding using virtual and override keywords −
public class BaseClass {
public virtual ReturnType MethodName() {
// base implementation
}
}
public class DerivedClass : BaseClass {
public override ReturnType MethodName() {
// derived implementation
}
}
Example
using System;
class Shape {
protected int width, height;
public Shape(int a = 0, int b = 0) {
width = a;
height = b;
}
public virtual int Area() {
Console.WriteLine("Parent class area:");
return 0;
}
}
class Rectangle : Shape {
public Rectangle(int a = 0, int b = 0) : base(a, b) { }
public override int Area() {
Console.WriteLine("Rectangle class area:");
return (width * height);
}
}
class Triangle : Shape {
public Triangle(int a = 0, int b = 0) : base(a, b) { }
public override int Area() {
Console.WriteLine("Triangle class area:");
return (width * height / 2);
}
}
class Caller {
public void CallArea(Shape sh) {
int a = sh.Area();
Console.WriteLine("Area: {0}", a);
}
}
class Program {
static void Main(string[] args) {
Caller c = new Caller();
Rectangle r = new Rectangle(10, 7);
Triangle t = new Triangle(10, 5);
c.CallArea(r);
c.CallArea(t);
}
}
The output of the above code is −
Rectangle class area: Area: 70 Triangle class area: Area: 25
Late Binding vs Early Binding
| Late Binding | Early Binding |
|---|---|
| Method resolution at runtime | Method resolution at compile time |
| Uses virtual and override keywords | Uses method overloading |
| Supports polymorphism | No polymorphic behavior |
| Slightly slower due to runtime lookup | Faster execution |
Common Use Cases
-
Framework design: Base classes define contracts while derived classes provide specific implementations.
-
Plugin architectures: Different plugins can override base functionality.
-
UI frameworks: Different controls override drawing or event handling methods.
Conclusion
Late binding in C# enables dynamic polymorphism by resolving method calls at runtime based on the actual object type. This powerful feature allows you to write flexible code where the same interface can exhibit different behaviors depending on the concrete implementation, making your applications more extensible and maintainable.
