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 compile time polymorphism in C#?
Compile-time polymorphism in C# is a type of polymorphism where the method to be called is determined during compilation rather than at runtime. It is also known as static polymorphism or early binding, because the method binding occurs at compile time.
C# provides two main techniques to implement compile-time polymorphism − Method Overloading and Operator Overloading. The compiler resolves which method to call based on the method signature (method name, number of parameters, and parameter types).
Method Overloading
Method overloading allows multiple methods with the same name but different signatures (different number or types of parameters) within the same class. The compiler determines which method to call based on the arguments passed.
Syntax
public returnType MethodName(type1 param1) { }
public returnType MethodName(type2 param2) { }
public returnType MethodName(type1 param1, type2 param2) { }
Example
using System;
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;
}
}
class Program {
static void Main(string[] args) {
Calculator calc = new Calculator();
Console.WriteLine("Adding two integers: " + calc.Add(5, 10));
Console.WriteLine("Adding two doubles: " + calc.Add(5.5, 10.3));
Console.WriteLine("Adding three integers: " + calc.Add(5, 10, 15));
}
}
The output of the above code is −
Adding two integers: 15 Adding two doubles: 15.8 Adding three integers: 30
Method Overloading with Different Parameter Types
Example
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();
p.print(5);
p.print(500.263);
p.print("Hello C#");
}
}
The output of the above code is −
Printing int: 5 Printing double: 500.263 Printing string: Hello C#
Operator Overloading
Operator overloading allows you to redefine operators for user-defined types. The compiler resolves which operator implementation to use at compile time.
Example
using System;
class Point {
public int X, Y;
public Point(int x, int y) {
X = x;
Y = y;
}
public static Point operator +(Point p1, Point p2) {
return new Point(p1.X + p2.X, p1.Y + p2.Y);
}
public override string ToString() {
return $"({X}, {Y})";
}
}
class Program {
static void Main(string[] args) {
Point p1 = new Point(3, 4);
Point p2 = new Point(1, 2);
Point p3 = p1 + p2;
Console.WriteLine($"Point 1: {p1}");
Console.WriteLine($"Point 2: {p2}");
Console.WriteLine($"Sum: {p3}");
}
}
The output of the above code is −
Point 1: (3, 4) Point 2: (1, 2) Sum: (4, 6)
Compile-time vs Runtime Polymorphism
| Compile-time Polymorphism | Runtime Polymorphism |
|---|---|
| Method resolved at compile time | Method resolved at runtime |
| Uses method overloading and operator overloading | Uses method overriding and virtual methods |
| Also called static polymorphism | Also called dynamic polymorphism |
| Faster execution (no runtime overhead) | Slower execution (runtime method resolution) |
Conclusion
Compile-time polymorphism in C# provides method flexibility through overloading while maintaining performance benefits since method resolution occurs during compilation. It enables cleaner, more intuitive code by allowing the same method name to handle different parameter types or counts.
