Monday, 17 November 2025

Dynamic Binding in C#

Dynamic Binding in C#

Dynamic Binding

Dynamic binding, also known as late binding, means that the method to execute is determined at runtime based on the actual object being referred to. It is used in runtime polymorphism.

Key Points:

  • Resolved at runtime.
  • Occurs during method overriding.
  • Requires virtual and override.
  • Base class reference can call derived class methods.

C# Example:

class Car { public virtual void Drive() { Console.WriteLine("Car is driving."); } } class SportsCar : Car { public override void Drive() { Console.WriteLine("SportsCar is driving fast!"); } } class Program { static void Main() { Car c = new SportsCar(); c.Drive(); } }

Output:

SportsCar is driving fast!

Function Overriding in C#

Function Overriding in C#

Function Overriding

Function overriding means defining the same method in a derived class that already exists in the base class, using the same name, parameters, and return type. It is used to change the inherited behavior.

Key Points

  • Requires virtual in the base class.
  • Requires override in the derived class.
  • Method name, parameters, and return type must match exactly.
  • Used for runtime polymorphism.

C# Example:

class Car { public virtual void Drive() { Console.WriteLine("Car is driving."); } } class SportsCar : Car { public override void Drive() { Console.WriteLine("SportsCar is driving very fast!"); } } class Program { static void Main() { Car c = new SportsCar(); c.Drive(); } }

Output:

SportsCar is driving very fast!

Operator Overloading in C#

Operator Overloading in C#

Operator Overloading in C#

Operator overloading allows developers to redefine the behavior of operators (like +, -, *, ==) for custom classes and structs. It makes objects behave like built-in types when using operators.

Key Points:

  • Uses the operator keyword.
  • Must be defined inside a class or struct.
  • At least one parameter must be a user-defined type.

C# Example:

class CarSpeed { public int Speed { get; set; } public CarSpeed(int speed) { Speed = speed; } public static CarSpeed operator +(CarSpeed c1, CarSpeed c2) { return new CarSpeed(c1.Speed + c2.Speed); } } class Program { static void Main() { CarSpeed car1 = new CarSpeed(60); CarSpeed car2 = new CarSpeed(40); CarSpeed total = car1 + car2; Console.WriteLine("Total Speed: " + total.Speed); } }

Output:

Total Speed: 100

Overloading Main Method

Overloading Main Method

Can You Overload the Main Method in C#?

Yes, you can overload the Main method in C#, but only one serves as the program's entry point.

Example:

class Program { static void Main(string[] args) { Console.WriteLine("Main with string[] args"); Main(10); } static void Main(int number) { Console.WriteLine("Overloaded Main with int: " + number); } static void Main(double value) { Console.WriteLine("Overloaded Main with double: " + value); } }

Key Points:

  • You can overload Main like any other method.
  • Only one method is used as the entry point.
  • Overloaded Main methods must be called manually.

Compile-time Polymorphism

Compile-time Polymorphism

Compile-time Polymorphism in C#

Compile-time polymorphism, also called static polymorphism, means that the method to be executed is decided during compile time. It is mainly achieved through method overloading in C#.

Features:

  • Decided at compile time
  • Achieved using method overloading
  • Occurs within the same class
  • Faster than runtime polymorphism

C# Example:

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() { Calculator c = new Calculator(); Console.WriteLine(c.Add(2, 3)); Console.WriteLine(c.Add(2.5, 3.1)); Console.WriteLine(c.Add(1, 2, 3)); } }

Runtime Polymorphism

Runtime Polymorphism

Runtime Polymorphism in C#

Runtime polymorphism (dynamic polymorphism) means that the method to be executed is decided during runtime. It is achieved through method overriding using the virtual and override keywords.

Features:

  • Achieved using method overriding
  • Uses virtual and override
  • Method selection happens at runtime
  • Enables dynamic and flexible behavior

C# Example:

class Car { public virtual void Drive() { Console.WriteLine("Car is driving."); } } class SportsCar : Car { public override void Drive() { Console.WriteLine("SportsCar is driving fast!"); } } class Program { static void Main() { Car c = new SportsCar(); c.Drive(); } }

Output:

SportsCar is driving fast!

Overloading vs Overriding

Overloading vs Overriding

Difference Between Overloading and Overriding in C#

1. Method Overloading

  • Same method name, different parameters.
  • Occurs within the same class.
  • Compile-time (static) polymorphism.
  • Parameters must change (type, number, or order).

C# Example:

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; } }

2. Method Overriding

  • Same method name, same parameters.
  • Occurs in different classes using inheritance.
  • Runtime (dynamic) polymorphism.
  • Uses virtual and override keywords.

C# Example:

class Car { public virtual void Drive() { Console.WriteLine("Car is driving"); } } class SportsCar : Car { public override void Drive() { Console.WriteLine("SportsCar is driving fast!"); } }