In overloading, there are multiple methods with different method signatures.
Below are some examples that show how we can achieve overloading by varying the number of parameters, the order of parameters, and data types of parameters.
Example 1:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 | using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace OverloadingExample { class Demo { public int Sum(int x, int y) { int value = x + y; return value; } public int Sum(int x, int y, int z) { int value = x + y + z; return value; } public static void Main(string[] args) // main method { Demo d = new Demo(); int sum1 = d.Sum(24, 28); Console.WriteLine("sum of the two " + "integer value : " + sum1); int sum2 = d.Sum(10, 20, 30); Console.WriteLine("sum of the three " + "integer value : " + sum2); Console.ReadLine(); } } } |
In the above example, there are two methods with the same name but a different number of parameters. The first method consists of two parameters while the second one has three parameters. This is called method overloading.
Output:
1 2 3 4 | sum of the two integer value : 52 sum of the three integer value : 60 |
Example 2:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 | using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace OverloadingExample { class Demo { public int Sum(int x, int y, int z) { int value = x + y + z; return value; } public double Sum(double x, double y, double z) { double value = x + y + z; return value; } public static void Main(string[] args) // main method { Demo d = new Demo(); int sum1 = d.Sum(24, 28,7); Console.WriteLine("sum of the two " + "integer value : " + sum1); double sum2 = d.Sum(10.0, 20.0, 30.0); Console.WriteLine("sum of the three " + "integer value : " + sum2); Console.ReadLine(); } } } |
In the above example, there are two methods with the same name but their data types are different. The first method has an integer data type while the second has a double data type. So in this case parameters are varying because of the different datatype.
Output:
1 2 3 4 | sum of the two integer value : 59 sum of the three integer value : 60 |
Example 3:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 | using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace OverloadingExample { class Demo { public void Details(String name,int id) { Console.WriteLine("Name " + name + ", " + "Id " + id); ; } public void Details(int id,string name) { Console.WriteLine("Name " + name + ", " + "Id " + id); } public static void Main(string[] args) // main method { Demo d = new Demo(); d.Details("John", 10); d.Details("Joe", 20); Console.ReadLine(); } } } |
In the above example, the name of the methods are the same but the order of parameters are different. The first method has a name and id resp. whereas the second one has id and name respectively.
Points to Remember:
In an overloading concept, it is not possible to define more than one method with the same parameters in case of order, type and number.
It is not possible to overload a method based on the different return types.