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 early binding in C#?
Early binding in C# is the mechanism of linking a method call with its implementation during compile time. It is also called static binding because the method to be called is determined at compilation rather than at runtime.
In early binding, the compiler knows exactly which method will be executed based on the method signature (name, parameters, and their types). This results in faster execution since no runtime resolution is needed.
How Early Binding Works
C# achieves early binding through static polymorphism, which includes method overloading and operator overloading. The compiler resolves which specific method to call based on the arguments provided at compile time.
Method Overloading Example
Method overloading is a prime example of early binding. The compiler selects the appropriate method based on the number and types of parameters −
using System;
class PrintData {
void Print(int i) {
Console.WriteLine("Printing int: {0}", i);
}
void Print(double f) {
Console.WriteLine("Printing double: {0}", f);
}
void Print(string s) {
Console.WriteLine("Printing string: {0}", s);
}
static void Main(string[] args) {
PrintData p = new PrintData();
// Compiler determines which Print method to call
p.Print(5); // calls Print(int)
p.Print(500.263); // calls Print(double)
p.Print("Hello C#"); // calls Print(string)
}
}
The output of the above code is −
Printing int: 5 Printing double: 500.263 Printing string: Hello C#
Operator Overloading Example
Operator overloading is another form of early binding where operators are resolved at compile time −
using System;
class Complex {
public int Real { get; set; }
public int Imaginary { get; set; }
public Complex(int real, int imaginary) {
Real = real;
Imaginary = imaginary;
}
public static Complex operator +(Complex c1, Complex c2) {
return new Complex(c1.Real + c2.Real, c1.Imaginary + c2.Imaginary);
}
public override string ToString() {
return $"{Real} + {Imaginary}i";
}
static void Main(string[] args) {
Complex num1 = new Complex(3, 4);
Complex num2 = new Complex(2, 1);
Complex result = num1 + num2; // Operator resolved at compile time
Console.WriteLine($"Result: {result}");
}
}
The output of the above code is −
Result: 5 + 5i
Early Binding vs Late Binding
| Early Binding | Late Binding |
|---|---|
| Method resolution at compile time | Method resolution at runtime |
| Faster execution | Slower execution due to runtime lookup |
| Type checking at compile time | Type checking at runtime |
| Examples: Method overloading, Operator overloading | Examples: Virtual methods, Interface methods |
Conclusion
Early binding in C# provides compile-time method resolution, resulting in faster execution and type safety. It is implemented through static polymorphism techniques like method overloading and operator overloading, where the compiler determines the exact method to call based on the method signature.
