why to use method overloading instead of creating different methods

In c# we use method overloading.
If we create a method with different names will also work the same.
so why to use method overloading?
sarthakmAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Ryan ChongBusiness Systems Analyst , ex-Senior Application EngineerCommented:
>>so why to use method overloading?
so to categorize the similar functionalities that could fulfill a task, with minimal differentiation.

just imagine if you got too many function names then it's difficult for users to remember. Categorize the similar functionalities also could mean ease for documentation.

read this article for further explanation:

Member Overloading
https://msdn.microsoft.com/en-us/library/vstudio/ms229029(v=vs.100).aspx

Overloaded members should provide variations on the same functionality.

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Jacques Bourgeois (James Burger)PresidentCommented:
If 2 (or more) methods do exactly the same thing, it simply makes sense that they have the same name.

It's the same thing as in the common language.

You can jump 10 feet high
You can jump back
You can jump out of a car

Would it make sense to have 3 verbs such as jumpdistance, jumpdirection and jumpfromsomething?

Not at all. It's easier always use the same verb, with different ways of describing the jump.

It's easier to always use the same method with different parameters than having many methods that to the same thing.

Think how difficult it would be to learn a language in which there are 10 verbs to describe the same action. The same goes for computer languages.
Prakash SamariyaIT ProfessionalCommented:
Good example, I want to add values (addition) Like : 10+10, 15+54+48, 48+45+56
we want additions, it may be parameters are 2/3/4/.... etc

So it is not good to create method like
Addition2parameter(a,b), then for 3
Addition3parameter(a,b,c)

What we want logically, there should be one method which can accept either 2/3/4/.... etc and do its respected activities like
Addition(a,b) then
Addition(a,b,c) then
Addition(a,b,c,d) and so on....

When two or more members have the SAME name and different parameter lists, the member is said to be overloaded.
_TAD_Commented:
It is true that many different function names could have the same effect, but method overloading is far more elegant - *AND* given the different method signatures, they really are kind of like different/unique functions.


The best example I can think of is string.Substring.  

string Substring ( int startIndex );
string SubString ( int startIndex, int length );


In this example you wouldn't need to code two separate and distinct functions, you can actually have one function call its own override.

Using Prakash's example of addition above, you can do something like:

public static int Addition ( int a, int b){
  return Addition(a, b, 0);  // hard-code a default for the third param and call overloaded method
}
public static int Addition ( int a, int b, int c){
  return Addition(a, b, c, 0);   // hard-code a default for the fourth param and call overloaded method
}
public static int Addition ( int a, int b, int c, int d ){
  return a + b + c + d;
}

Open in new window

It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
.NET Programming

From novice to tech pro — start learning today.