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
Static binding vs Dynamic binding in C#
Polymorphism can be static or dynamic. In static polymorphism, the method to be called is determined at compile time. In dynamic polymorphism, the decision is made at runtime based on the actual object type.
Static Binding (Compile-Time Polymorphism)
Static binding links a function with an object during compile time. It is also called early binding. This is achieved through method overloading and operator overloading.
Example - Method Overloading
using System;
public class Calculator {
public int Add(int a, int b) {
return a + b;
}
public double Add(double a, double b) {
return a + b;
}
public int Add(int a, int b, int c) {
return a + b + c;
}
}
public class Program {
public static void Main() {
Calculator calc = new Calculator();
Console.WriteLine("Integer addition: " + calc.Add(5, 10));
Console.WriteLine("Double addition: " + calc.Add(5.5, 10.3));
Console.WriteLine("Three integers: " + calc.Add(1, 2, 3));
}
}
The output of the above code is −
Integer addition: 15 Double addition: 15.8 Three integers: 6
Dynamic Binding (Runtime Polymorphism)
Dynamic binding determines which method to call at runtime based on the actual object type. It is also called late binding. This is implemented using virtual methods, abstract classes, and method overriding.
Example - Virtual Methods and Overriding
using System;
public class Animal {
public virtual void MakeSound() {
Console.WriteLine("Animal makes a sound");
}
}
public class Dog : Animal {
public override void MakeSound() {
Console.WriteLine("Dog barks: Woof!");
}
}
public class Cat : Animal {
public override void MakeSound() {
Console.WriteLine("Cat meows: Meow!");
}
}
public class Program {
public static void Main() {
Animal animal1 = new Dog();
Animal animal2 = new Cat();
Animal animal3 = new Animal();
animal1.MakeSound(); // Calls Dog's method
animal2.MakeSound(); // Calls Cat's method
animal3.MakeSound(); // Calls Animal's method
}
}
The output of the above code is −
Dog barks: Woof! Cat meows: Meow! Animal makes a sound
Example - Abstract Classes
using System;
public abstract class Shape {
public abstract double CalculateArea();
public void DisplayInfo() {
Console.WriteLine($"Area: {CalculateArea():F2}");
}
}
public class Circle : Shape {
private double radius;
public Circle(double r) { radius = r; }
public override double CalculateArea() {
return Math.PI * radius * radius;
}
}
public class Rectangle : Shape {
private double width, height;
public Rectangle(double w, double h) {
width = w;
height = h;
}
public override double CalculateArea() {
return width * height;
}
}
public class Program {
public static void Main() {
Shape shape1 = new Circle(5);
Shape shape2 = new Rectangle(4, 6);
shape1.DisplayInfo();
shape2.DisplayInfo();
}
}
The output of the above code is −
Area: 78.54 Area: 24.00
Comparison
| Static Binding | Dynamic Binding |
|---|---|
| Method resolution at compile time | Method resolution at runtime |
| Uses method overloading | Uses method overriding |
| Faster execution (no runtime overhead) | Slightly slower (runtime method lookup) |
| Also called early binding | Also called late binding |
| Examples: overloaded methods, operators | Examples: virtual methods, abstract methods |
Conclusion
Static binding resolves method calls at compile time using method overloading, providing faster execution. Dynamic binding resolves method calls at runtime using virtual methods and overriding, enabling true polymorphism where the actual object type determines which method executes.
