C# Method Overloading

C# Method Overloading: Using the Same Method Name

Method Overloading is a powerful feature in C# that allows multiple methods to share the same name, as long as they have different parameters.

This makes your code cleaner, more intuitive, and easier to use.


1. What is Method Overloading?

Normally, you cannot have two methods with the exact same name in the same class. The compiler wouldn't know which one you are trying to call!

However, if the methods have a different signature (meaning they take different types or numbers of parameters), the C# compiler is smart enough to figure out which one you want based on the arguments you pass in.

Consider the following example where we want to add numbers. Without overloading, we might have to create differently named methods for different data types:

int AddIntegers(int x, int y) {
  return x + y;
}

double AddDoubles(double x, double y) { return x + y; }

Having to remember AddIntegers and AddDoubles is tedious. Method overloading solves this elegantly.


2. Example: Overloading by Data Type

With method overloading, multiple methods can have the same name with different parameters:

Example: Method Overloading

using System;

namespace MyApplication { class Program { // Method for integers static int PlusMethod(int x, int y) { return x + y; } // Method for doubles (same name!) static double PlusMethod(double x, double y) { return x + y; } static void Main(string[] args) { int myNum1 = PlusMethod(8, 5); double myNum2 = PlusMethod(4.3, 6.26); Console.WriteLine("Int: " + myNum1); Console.WriteLine("Double: " + myNum2); } } }

When you call PlusMethod(8, 5), the compiler sees you passed two int values, so it executes the integer version. When you pass 4.3 and 6.26, it executes the double version!


3. Example: Overloading by Number of Parameters

You can also overload methods by changing the number of parameters they accept.

Example: Different Number of Parameters

using System;

namespace MyApplication { class Program { static void PrintInfo(string name) { Console.WriteLine("Name: " + name); } static void PrintInfo(string name, int age) { Console.WriteLine("Name: " + name + ", Age: " + age); } static void Main(string[] args) { PrintInfo("Alice"); // Calls the first method PrintInfo("Bob", 25); // Calls the second method } } }


4. The Method Signature Rule

For method overloading to work, the methods must have different signatures. A method signature consists of:

  1. The method name.
  2. The number of parameters.
  3. The types of parameters.
  4. The order of the parameters.

Crucial Note: The return type of a method is not part of its signature. You cannot overload two methods if they only differ by their return type. Doing so will result in a compiler error.

Invalid Overloading Example:

// This will cause a compiler error!
static int Calculate(int x) { return x; }
static double Calculate(int x) { return x; } 
Because both methods take a single `int` as a parameter, the compiler wouldn't know which one to pick if you called `Calculate(5)`.

Exercise

?

Method overloading allows multiple methods to have the same name as long as they have...