Monday, 17 November 2025

Method Hiding in C#

Method Hiding in C#

Method Hiding

Method hiding occurs when a derived class defines a method with the same name as a method in the base class but uses the new keyword. The version of the method called depends on the reference type.

C# Example:

using System; class Car { public void Start() { Console.WriteLine("Car is starting."); } } class SportsCar : Car { public new void Start() { Console.WriteLine("SportsCar is starting with turbo!"); } } class Program { static void Main() { Car carRef = new Car(); carRef.Start(); SportsCar sportsCarRef = new SportsCar(); sportsCarRef.Start(); Car baseRef = new SportsCar(); baseRef.Start(); } }

Output:

Car is starting. SportsCar is starting with turbo! Car is starting.

Function Overriding Example in C#

Function Overriding Example in C#

Function Overriding Example

In this example, the Start method is overridden in the derived class SportsCar using the override keyword. This demonstrates runtime polymorphism.

C# Example:

using System; class Car { public virtual void Start() { Console.WriteLine("Car is starting."); } } class SportsCar : Car { public override void Start() { Console.WriteLine("SportsCar is starting with turbo!"); } } class Program { static void Main() { Car myCar = new Car(); myCar.Start(); SportsCar mySportsCar = new SportsCar(); mySportsCar.Start(); Car carRef = new SportsCar(); carRef.Start(); } }

Output:

Car is starting. SportsCar is starting with turbo! SportsCar is starting with turbo!

Static Binding in C#

Static Binding in C#

Static Binding

Static binding, also called early binding, means that the method to be executed is determined at compile time. It is used in compile-time polymorphism such as method overloading and operator overloading.

Key Points:

  • Resolved at compile time.
  • Occurs in method overloading or operator overloading.
  • Faster than dynamic binding.
  • Opposite of dynamic (late) binding.

C# Example:

class Calculator { public int Add(int a, int b) { return a + b; } public double Add(double a, double b) { return a + b; } } class Program { static void Main() { Calculator calc = new Calculator(); Console.WriteLine(calc.Add(2, 3)); Console.WriteLine(calc.Add(2.5, 3.5)); } }

Output:

5 6

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.