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
public class Calculate
{
public Int32 Add(Int32 num1, Int32 num2)
{
return num1 + num2;
}
//Now create another method with same name Add but with two double
public double Add(double dbl1, double dbl2)
{
return dbl1 + dbl2;
}
//Create another method to accept array of integer and return long
public long Add(Int32[] numbers)
{
long result = 0;
for (Int32 i = 0; i < numbers.Length; i++)
result += numbers[i];
return result;
}
//Create a method to accept array of double and return double
public double Add(double[] doubles)
{
double result = 0;
for (Int32 i = 0; i < doubles.Length; i++)
result += doubles[i];
return result;
}
}
Now we will test our class one by one
class Program
{
static void Main(string[] args)
{
Calculate cal = new Calculate();
Int32 twoNumberResult = cal.Add(10, 20);
Console.WriteLine(twoNumberResult); // It will print 30
double twoDoubleResult = cal.Add(10.25, 20.30);
Console.WriteLine(twoDoubleResult); // It will print 30.55
Int32[] intArray = { 10, 20, 30, 40, 50 };
long intArrayResult = cal.Add(intArray);
Console.WriteLine(intArrayResult); // It will print 150
double[] doubleArray = { 10.11, 20.22, 30.33, 40.44, 50.55 };
double doubleArrayResult = cal.Add(doubleArray);
Console.WriteLine(doubleArrayResult); // It will print 151.65
Console.ReadLine();
}
}
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
We can not overload a method
![]() |
Process manager with a reputed organization, Fond of learning new features and technology related to C#, ASP.Net, SQL Server, MVC etc.I like to help others, if I can
|
By Hamden On 19 Jul, 13 Viewed: 3,143 |
In my early day career in many interviews I asked same question, what is overloading and overriding so decided to write this small article which may help someone. We will see here in detail about the overloading, overriding and then differences between them. --- **Overloading:** --- is the... By Hamden On 31 Mar 2013 Viewed: 34,626
In C# there are different data type and they use some default value when we declare a variable. When we define a variable of type int or Int32 say int score; so what is the value of score, will it be null or zero, in the same way if we create a variable of type string/String what value it holds... By Ali Adravi On 27 Mar 2013 Viewed: 2,615
An enumeration type (also known an enumeration or an enum) provides an efficient way to define a set of named integral constants that may be assigned to a variable to make programming clear, understandable and manageable. There are ways of using, like how to use enum in switch and case statement,... By Hamden On 27 Mar 2013 Viewed: 4,571
When we have var which dynamically holds any type or value so why we need dynamic type, what are the differences and situations where we can use dynamic rather than var, there were some question in my mind so I explored more which I want to share here. There are two things, static type and... By Nathan Armour On 26 Mar 2013 Viewed: 3,219
An array can be initialized in several ways, it can store a single value or series of values, every element must be set of same type, C# supports single-dimensional arrays, multidimensional arrays (also call rectangular array) and array-of-arrays (referred jagged arrays), let’s discuss features of... By Ali Adravi On 24 Mar 2013 Viewed: 4,379