Monday 6 January 2020

Method overloading in C#

Method overloading means having different methods with same name but with different types of parameters or number of parameters also known as static polymorphism. In this article we will try to understand what is method overloading and how we can overload a method with example.
Suppose we have a class Calculate which can have a method Add by using two integers, two double, any number of integer, any number of double etc. so let’s see it in action by creating the customer class
  1. public class Calculate
  2. {
  3.     public Int32 Add(Int32 num1, Int32 num2)
  4.     {
  5.         return num1 + num2;
  6.     }
  7.     //Now create another method with same name Add but with two double
  8.     public double Add(double dbl1, double dbl2)
  9.     {
  10.         return dbl1 + dbl2;
  11.     }
  12.     //Create another method to accept array of integer and return long
  13.     public long Add(Int32[] numbers)
  14.     {
  15.       long result = 0;
  16.       for (Int32 i = 0; i < numbers.Length; i++)
  17.          result += numbers[i];
  18.       return result;
  19.     }
  20.     //Create a method to accept array of double and return double
  21.     public double Add(double[] doubles)
  22.     {
  23.       double result = 0;
  24.       for (Int32 i = 0; i < doubles.Length; i++)
  25.          result += doubles[i];
  26.       return result;
  27.     }
  28. }
Now we will test our class one by one
  1. class Program
  2. {
  3.     static void Main(string[] args)
  4.     {
  5.         Calculate cal = new Calculate();
  6.         Int32 twoNumberResult = cal.Add(10, 20);
  7.         Console.WriteLine(twoNumberResult); // It will print 30
  8.         double twoDoubleResult = cal.Add(10.25, 20.30);
  9.         Console.WriteLine(twoDoubleResult); // It will print 30.55
  10.         Int32[] intArray = { 10, 20, 30, 40, 50 };
  11.         long intArrayResult = cal.Add(intArray);
  12.         Console.WriteLine(intArrayResult); // It will print 150
  13.         double[] doubleArray = { 10.11, 20.22, 30.33, 40.44, 50.55 };
  14.         double doubleArrayResult = cal.Add(doubleArray);
  15.         Console.WriteLine(doubleArrayResult); // It will print 151.65
  16.         Console.ReadLine();
  17.     }
  18. }
If you want to test the above code just create a console application and copy and page the code and run it.
Let's see when we can overload a method when to not.
We can overload a method
  1. With different types of parameter
  2. with different number of parameter
  3. with same number of parameter but of different type
  4. with same number and type of parameter but in different sequence
We can not overload a method
  1. with different return type only
  2. with different access modifier only say (public, private, protected etc.)
  3. with regular and optional parameter

No comments:

Post a Comment

Baisic Useful Git Commands

  Pushing a fresh repository Create a fresh repository(Any cloud repository). Open terminal (for mac ) and command (windows) and type the be...