2 minute read

Method Overloading in C#

In this article we will learn about Method Overloading which is an example of polymorphism, a pillar of object oriented programming. Method overloading is the ability to redefine a method more than one times, that is creating a multiple methods in a class that share the same name. The overloaded methods can have the same name but with different signatures. Method signatures simply means that the number and type of parameters passed as arguments. Method overloading can be done by changing the following:

  • The number of parameters
  • The data types of the parameters
  • The order of the parameters

The number of parameters

class MultiplyNumbers
    {
        public int Product(int a, int b)
        {
            int product = a * b;
            return product;
        }
        public int Product(int a, int b, int c)
        {
            return a * b * c;
        }
   
        public static void Main(string[] args)
        {
            MultiplyNumbers multiplyNumbers = new MultiplyNumbers();
            int product = multiplyNumbers.Product(5, 6);

            Console.WriteLine($"The product of the two numbers is: {product}");

            MultiplyNumbers multiplyNumbers2 = new MultiplyNumbers();
            int product2 = multiplyNumbers2.Product(5, 6, 7);

            Console.WriteLine($"The product of the two numbers is: {product}");
        }

The output

The product of the two numbers is: 30

The product of the two numbers is: 210

The data types of parameters

    class MultiplyNumbers
    {
        public float Product(float a, float b)
        {
            return a * b;
        }
        public int Product(int a, int b)
        {
            return a * b;
        }
   
        public static void Main(string[] args)
        {
            MultiplyNumbers multiplyNumbers = new MultiplyNumbers();
            float product = multiplyNumbers.Product(5.67F, 6.2F);

            Console.WriteLine($"The product of the two numbers is: {product}");

            MultiplyNumbers multiplyNumbers2 = new MultiplyNumbers();
            int product2 = multiplyNumbers2.Product(5, 6);

            Console.WriteLine($"The product of the two numbers is: {product}");
        }
    }
The output

The product of the two numbers is: 35.154

The product of the two numbers is: 30

The number of parameters

// C# program to demonstrate the method
// overloading by changing the
// Order of the parameters

using System;

    // C# program to demonstrate the function
    // overloading by changing the
    // Order of the parameters

    class Employee
    {
        public void Identity(String name, int id)
        {

            Console.WriteLine($"Name:{name}, ID:{id}");
        }

        public void Identity(int id, String name)
        {

            Console.WriteLine($"Name:{name}, ID:{id}");
        }


        public static void Main(string[] args)
        {
            Employee emp1 = new Employee();
            emp1.Identity("John Doe", 1);

            Employee emp2 = new Employee();
            emp1.Identity(2, "Mary Doe");
        }
    }

Name : John Doe, ID : 1

ID : 2, Name : Mary Doe

Why we need Method Overloading

Coming up with different Method names that perform the same operation can be a challenge. Method overloading is important when we need to perform the same operation in different ways for different inputs.

Thank You!

I’d love to keep in touch! Feel free to follow me on Twitter at @codewithfed. If you enjoyed this article please consider sponsoring me for more. Your feedback is welcome anytime. Thanks again!

![][image_ref_h8627dve]

Leave a comment