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 static binding in C#?
Static binding in C# refers to the process of linking a function call to its implementation at compile time. This is also known as early binding or compile-time polymorphism. The compiler determines which method to call based on the method signature and the types of arguments passed.
C# provides two main techniques to implement static binding through static polymorphism −
Function Overloading − Multiple methods with the same name but different parameters
Operator Overloading − Custom implementations for operators like +, -, *, etc.
Function Overloading
Function overloading allows you to have multiple method definitions with the same name in the same scope, but with different parameter lists. The compiler selects the appropriate method based on the arguments provided −
Example
using System;
class Calculator {
public void Print(int i) {
Console.WriteLine("Printing int: {0}", i);
}
public void Print(double f) {
Console.WriteLine("Printing double: {0}", f);
}
public void Print(string s) {
Console.WriteLine("Printing string: {0}", s);
}
}
class Program {
public static void Main() {
Calculator calc = new Calculator();
calc.Print(5);
calc.Print(3.14);
calc.Print("Hello");
}
}
The output of the above code is −
Printing int: 5 Printing double: 3.14 Printing string: Hello
Operator Overloading
Operator overloading allows you to define custom behavior for operators when used with user-defined types. The overloaded operator is defined using the operator keyword followed by the operator symbol −
Syntax
public static ReturnType operator symbol (parameters) {
// implementation
}
Example
using System;
class Box {
public double length, breadth, height;
public Box() { }
public Box(double l, double b, double h) {
length = l;
breadth = b;
height = h;
}
public static Box operator+ (Box b1, Box b2) {
Box box = new Box();
box.length = b1.length + b2.length;
box.breadth = b1.breadth + b2.breadth;
box.height = b1.height + b2.height;
return box;
}
public void Display() {
Console.WriteLine("Length: {0}, Breadth: {1}, Height: {2}",
length, breadth, height);
}
}
class Program {
public static void Main() {
Box box1 = new Box(2.0, 3.0, 4.0);
Box box2 = new Box(1.0, 2.0, 3.0);
Box box3 = box1 + box2;
Console.WriteLine("Box1:");
box1.Display();
Console.WriteLine("Box2:");
box2.Display();
Console.WriteLine("Box3 (Box1 + Box2):");
box3.Display();
}
}
The output of the above code is −
Box1: Length: 2, Breadth: 3, Height: 4 Box2: Length: 1, Breadth: 2, Height: 3 Box3 (Box1 + Box2): Length: 3, Breadth: 5, Height: 7
Static Binding vs Dynamic Binding
| Static Binding | Dynamic Binding |
|---|---|
| Method call resolved at compile time | Method call resolved at runtime |
| Faster execution | Slower execution due to runtime lookup |
| Used in method overloading | Used in method overriding with virtual/override |
| No inheritance required | Requires inheritance and polymorphism |
Conclusion
Static binding in C# provides compile-time method resolution through function overloading and operator overloading. It offers better performance since method calls are resolved during compilation, eliminating the need for runtime method lookup that occurs with dynamic binding.
