Saturday, July 23, 2016

Method Overloading in C#


In C#, when we use the same method name but different “no. of parameter/ data type of parameter” then it is called method overloading.


Example:
int sum(int a, int b){…}
int sum(int a, int b, int c) {…}
sum(float a, float b) {…}

above methods are overloaded.

Important Note:

   1.       Return type and access specifier are not part of method signature.

Example:
int sum(int a, int b){…}
private int sum(int a, int b){…}
public int sum(int a, int b) {…}
float sum(int a, int b) {…}

above methods are not overloaded.

   2.       It is called the compile time polymorphism or early binding or static binding.
   3.       Method having “Ref” and “Out” type parameters are not overloaded, if it has the same signatures excluding the “ref” and “out” keyword.

Example:
 sum(ref int a, ref int b){…}
 sum(out int a, out int b) {…}

above methods are not overloaded.

   4.       Method having “ref” or “out” parameter and another method having without “ref” or “out” are overloaded.

Example:
sum(ref int a, ref int b){…}
            sum(int a, int b){…}
         
above methods are overloaded.

Example:

Create Program.cs class file:

using System;

namespace Demo
{
    class Program
    {
        static void Main(string[] args)
        {
            MethodType mType = new MethodType();
            mType.sum();
            Console.ReadLine();
           
            int result = mType.sum(50, 200);
            Console.WriteLine(result);
            Console.ReadLine();
                       

            //example of call by reference by using ref
           
            int a1 = 10, b1 = 20;
            Console.WriteLine("before: value of a1 " + a1);
            Console.WriteLine("before: value of b1 " + b1);
            int sumResult2 = mType.sum(ref a1, ref b1);
            Console.WriteLine(sumResult2); //400           
            Console.WriteLine("after: value of a " + a1); //100
            Console.WriteLine("after: value of b " + b1); //700
            Console.ReadLine();

            //example of call by reference by using ref

            // example of call by reference by using out
            int a2, b2;
            int sumResult3 = mType.sumValueByOut(out a2, out b2);
            Console.WriteLine(sumResult3); //1500           
            Console.WriteLine("after: value of a2 " + a2); //1000
            Console.WriteLine("after: value of b2 " + b2); //500
            Console.ReadLine();
            // example of call by reference by using out
        }
    }
}

Create MethodType.cs class file:

using System;

namespace Demo
{
    class MethodType
    {
        public void sum()
        {
            int num1, num2, result;
            num1 = 100;
            num2 = 200;
            result = num1 + num2;
            Console.WriteLine(result);
            Console.WriteLine("sum of num1 and num2 is : " + result);
            Console.WriteLine("sum of {0} and {1} is : {2} :", num1, num2, result);
        }

        public int sum(int num1, int num2)
        {           
            int result;           
            result = num1 + num2;
            return result;
        }     

             
public int sum(ref int num1, ref int num2)
        {
            int result;
            num1 = 100;
            num2 = 700;
            result = num1 + num2;
            return result;
        }

        public int sumValueByOut(out int num1, out int num2)
        {
            int result;
            num1 = 1000;
            num2 = 500;
            result = num1 + num2;
            return result;
        }

    }
}

How to run the program:
Create a console application having its name as “Demo” in visual studio or other IDE that support the C#.net and run the application.

Thanks for reading this blog post. Please provide your feedback and suggestions.