Exploring Method Overloading

Tutorial 3 of 5

1. Introduction

In this tutorial, we will examine the concept of method overloading in C#. Method overloading allows you to use the same method name within the same scope for performing different tasks. This is achieved by altering the number or types of parameters.

By the end of this tutorial, you will be able to:

  • Understand the concept of method overloading.
  • Implement method overloading in C#.
  • Recognize scenarios where method overloading can be useful.

Prerequisites:

  • Basic knowledge of C# programming.
  • Familiarity with methods in C#.

2. Step-by-Step Guide

Method overloading is a feature that allows a class to have two or more methods sharing the same name, so long as their parameter lists are different. When we call an overloaded method, the C# compiler uses the argument list to determine the specific method to invoke.

Here are some conditions that must be met for overloading a method:

  • The parameter types must be different.
  • The parameter order can be different.

3. Code Examples

Example 1: Method Overloading by Changing Number of Parameters

public class Calculator
{
    // Method Add with 2 parameters
    public int Add(int a, int b)
    {
        return a + b;
    }

    // Method Add with 3 parameters
    public int Add(int a, int b, int c)
    {
        return a + b + c;
    }
}

class Program
{
    static void Main(string[] args)
    {
        Calculator calculator = new Calculator();

        Console.WriteLine(calculator.Add(1, 2)); // Output: 3
        Console.WriteLine(calculator.Add(1, 2, 3)); // Output: 6
    }
}

In the above example, we have two methods named Add. The first Add method takes two parameters and the second Add method takes three parameters. When we call Add method with two and three arguments, the corresponding methods get called.

Example 2: Method Overloading by Changing Type of Parameters

public class Printer
{
    // Method Print with int type parameter
    public void Print(int i)
    {
        Console.WriteLine("Printing int: " + i);
    }

    // Method Print with string type parameter
    public void Print(string s)
    {
        Console.WriteLine("Printing string: " + s);
    }
}

class Program
{
    static void Main(string[] args)
    {
        Printer printer = new Printer();

        printer.Print(5); // Output: Printing int: 5
        printer.Print("Hello"); // Output: Printing string: Hello
    }
}

In this example, we have two methods named Print, but their parameter types are different. The C# compiler uses the type of argument passed to determine the specific method to invoke.

4. Summary

Method overloading in C# is a powerful feature that allows us to use the same method name to perform different tasks, making our code cleaner and more readable. It's important to remember that overloaded methods must have different parameter lists.

For further practice, try creating your own classes with overloaded methods and invoking them. Also, try to understand why method overloading is useful and when to use it.

5. Practice Exercises

Exercise 1: Create a method Multiply that can multiply two or three numbers.

Exercise 2: Create a method Display that can display both string and integer inputs.

Exercise 3: Create a method CalculateArea that can calculate the area of a circle, rectangle, and triangle using method overloading.

Solutions:

//Solution for Exercise 1
public int Multiply(int a, int b) { return a * b; }
public int Multiply(int a, int b, int c) { return a * b * c; }

//Solution for Exercise 2
public void Display(int i) { Console.WriteLine("Displaying int: " + i); }
public void Display(string s) { Console.WriteLine("Displaying string: " + s); }

//Solution for Exercise 3
public double CalculateArea(int radius) { return 3.14 * radius * radius; } //Circle
public int CalculateArea(int length, int breadth) { return length * breadth; } //Rectangle
public double CalculateArea(int base, int height) { return 0.5 * base * height; } //Triangle

Remember, practice is key when learning programming. Keep experimenting with different scenarios and try to understand the underlying concepts.