Method Overloading in C#.NET

Introduction:

Method overloading is one of way to implement polymorphism.

Method can be define in more than one form. It is also called compile time binding.

 What is Method Overloading?

Creating different methods with same name but different signature is called method overloading.

 

Method Signature:

Method signature include followings:

  1. Number of parameters.
  2. Order of parameters
  3. Data types of parameters

Return type is not part of  method signature.

Why Method Overloading?

Let suppose you want to calculate sum of different numbers based on user input. Here we don’t know what type of user is going to enter number as input, then instead of creating different method name, create same method name i.e “Sum” can be used.

So when you want to do same kind of operation in different way then we can use method overloading.

 

  • Code for Number of parameters:
using System;

namespace MethodOverloading
{

    class Program
    {

        //Two parameter of int type
        public static int Sum(int x, int y)
        {
            return x + y;
        }
        //Three parameter of int type
        public static int Sum(int x,int y,int z)
        {
            return x + y + z;
        }

        static void Main()
        {

            Console.WriteLine("Sum of two integer numbers: {0}", Sum(10,20));
            Console.WriteLine("Sum of Three integer numbers: {0}", Sum(10, 20,30));

            Console.ReadLine();
        }

    }


}

Output:

image

 

  • Code for Order of Parameters:
using System;
namespace MethodOverloading
{
    class Program
    {
        //Two parameter of int and decimal type 
        public static int Sum(int x, decimal y)
        {
            return x + Convert.ToInt32(y);
        }
        //Three parameter of decimal and int type    
        public static int Sum(decimal x, int y)
        {
            return Convert.ToInt32(x) + y;
        }
        static void Main()
        {
            Console.WriteLine("Sum of int and decimal numbers: {0}", Sum(10, 20.5m));
            Console.WriteLine("Sum of decimal and int  numbers: {0}", Sum(10.5m, 20));
            Console.ReadLine();
        }
    }
}

Output:

image

 

  • Code for Data Types of Parameters:

using System;

namespace MethodOverloading
{
    
    class Program
    {

        //Two parameter of int type
        public static int Sum(int x, int y)
        {
            return x + y;
        } 
        //Three parameter of decimal type
        public static decimal Sum(decimal x,decimal y)
        {
            return x + y;
        }

        static void Main()
        {

            Console.WriteLine("Sum of two integer numbers: {0}", Sum(10,20));
            Console.WriteLine("Sum of two decimal  numbers: {0}", Sum(10.5m, 20.5m));

            Console.ReadLine();
        }

    }


}

Output:

image

 

  • Method with same signature but different return types:

using System;
namespace MethodOverloading
{
    class Program
    {
        //Two parameter of int and decimal type 
        public static int Sum(int x, decimal y)
        {
            return x + Convert.ToInt32(y);
        }
        //Two parameter of int and decimal type 
        public static decimal Sum(int x, decimal y)
        {
            return x + Convert.ToInt32(y);
        }

        static void Main()
        {
            Console.WriteLine("Sum of int and decimal numbers: {0}", Sum(10, 20.5m));
            Console.WriteLine("Sum of int and decimal numbers: {0}", Sum(10, 20.5m));
            Console.ReadLine();
        }
    }
}

Output:

Output is compile time error since method parameters are same but return type is different.

Return type is not consider for method overloading so both method are treated same by compiler hence compile time error is thrown.

 

image

 

Method overloading using Ref and out?

We cannot use Ref and Out for method overloading. Compiler treats same method when one method takes ref parameter and other method takes an out parameter.

                                     

  Example:

using System;
namespace MethodOverloading
{
    class Program
    {
        //Method with ref parameter
        public static int Show(ref int x)
        {
            return x + 100;
        }
        //Method with out parameter
        public static int Show(out int y)
        {
            y  = 100;
            
            return y;
        }

        static void Main()
        {
            int x = 10;
            int y;
            Console.WriteLine("{0}", Show(ref x));
            Console.WriteLine("{0}", Show(out y));
            Console.ReadLine();
        }
    }
}

Output:

Compiler is throwing error since both methods are treated as same here.

image

 

Method overloading is possible when one method takes a ref or out parameter and the other method takes simple parameters (except ref and out parameter like int, decimal etc).

 

Example:

using System;
namespace MethodOverloading
{
    class Program
    {
        //Method with ref parameter
        public static int Show(ref int x)
        {
            return x + 100;
        }
        //Method with int parameter
        public static int Show(int y)
        {   
            return y;
        }

        static void Main()
        {
            int x = 10;
            int y = 20;
            Console.WriteLine("{0}", Show(ref x));
            Console.WriteLine("{0}", Show(y));
            Console.ReadLine();
        }
    }
}

 

Output:

110

20

 

Conclusion:

    • Method having same name but different signature is called method overloading.
    • Number of parameter, Order of parameter and data types of parameter is consider for signature.
    • Return type is not consider for method signature
    • One method with ref and another method with out parameter will be treated as same method  by compiler and throws error at compile time.
    • If one method has ref/out and another method has simple parameter like int, float etc then it is method overloading.

 

Please provide you valuable feedback or comment to improve this post.

Happy coding Smile

Be the first to comment

Leave a Reply

Your email address will not be published.


*