42
1
using System;
2
3
public class OverloadingInCsharpProgram
4
{
5
public static void Main()
6
{
7
Overloading obj = new Overloading();
8
int twoNumberResult =obj.Add(10,20);
9
Console.WriteLine(twoNumberResult); // It will print 30
10
11
double twoDoubleSum = obj.Add(15.25, 20.30);
12
Console.WriteLine(twoDoubleSum); // It will print 35.55
13
14
int[] intArray = { 10, 20, 30, 40, 50 };
15
long intArrayResult = obj.Add(intArray);
16
Console.WriteLine(intArrayResult); // It will print 150
17
}
18
19
public class Overloading
20
{
21
//method 1 with simple int parameters to add int's
22
public int Add(int num1, int num2)
23
{
24
return num1 + num2;
25
}
26
27
//another method with same name Add but with two double as parameter
28
public double Add(double dbl1, double dbl2)
29
{
30
return dbl1 + dbl2;
31
}
32
33
// another method to accept array of integer and return long but with same name
34
public long Add(int[] numbers)
35
{
36
long result = 0;
37
for (Int32 i = 0; i < numbers.Length; i++)
38
result += numbers[i];
39
return result;
40
}
41
}
42
}
Cached Result