Click here to Skip to main content
15,890,557 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
weh have 2 overloaded methods

public int Add(int a,int b)
{
return a+b;
}

public int Add(int a,int b,int c)
{
return a+b+c;

}

Why to use Method over loading if we can do it in another way like :

Now Look at below:

weh have 2 overloaded methods

public int Addtwo(int a,int b)
{
return a+b;
}

public int Addthree(int a,int b,int c)
{
return a+b+c;

}


in My second example we can do the same operation as we did in first Example,so what is need of overloading the methon ,Please explain.
I had faced this question in interview,but unable to answer.
Posted

Asking "why" here is the same as asking an artist "why have you chosen this location for your landscape?". Because. Or, even better answer: "why not"?

Using "overloaded" methods is not a matter of technology, this is a matter of style. But ultimately, such decision, carrying no functional meaning, affect the readability and maintainability of code, in a negative or a positive way.

"Overloading" is a very misleading word: nothing is "overloaded" because nothing is loaded here. This is nothing but an additional freedom in the choice of method names: some methods can have the same name just because from the line where they one is called, it's possible to figure out which one, based on actual parameter passed (and sometime not possible, then the compilation error is shown; and the problem can be resolved by exact specification of parameter types). That's all.

All the methods you show are about addition. Unfortunately, they don't have any practical sense, so it would not make much sense to criticize or approve this naming. There are many more practical cases. Look at the .NET FCL methods: you will find a lot of good examples. I would just suggest you to think how the API would look with different naming, would it be better or worse. Think in term of usage; what happens, for example, if you change your mind and use another method instead of the one you used before. And so on.

—SA
 
Share this answer
 
What you mention is achieving the result. We can achieve the result in different ways. We can achieve the result even without writing the method. But as a developer you need to keep mind that a developed software should be maintained as well. That's reason we have all this concepts of OOPS, design patterns, etc used during development. I know I have not answered the question but is more generic answer for any developer.
 
Share this answer
 
v2
Comments
Sergey Alexandrovich Kryukov 25-Oct-13 2:52am    
It's good to understand that it all has nothing to do with OOP, design patterns, etc. It has nothing to do with any functionality at all.
And nothing is "overloaded" actually (the word which evoke wrong thinking that the feature is more functional then it really is).
There is a lot of practical and aesthetic meaning in it, but no functionality.
Please see my answer where I try to explain it.
—SA
Overloading is for code readability and maintainability & it's one of the OOP concept called polymorphism. You could find more details in web. Sample one here - C# Method Overloading[^]

But you could improve(reduce code) this using Named and Optional Arguments[^] which is great features.
 
Share this answer
 
It is for readability. It is also easier to locate function to use when functions that perform the same task have the same name but different parameters.
 
Share this answer
 
Hi,

In your example you are not observing any issue but when you have many number of overloaded methods that do the similar kind of task(not same). Example Console.WriteLine have 19 different overloaded methods. If you are using different name for each methods, there will be many methods will be displayed in intellisense(if you are used to VS) under Console class. Even you have to remember all different methods that are available to write content on console with different name. Instead in case of overloading you just need to remember single name for writing line on console.

You can read more details in SA's answer.

Best luck
-Amit
 
Share this answer
 
Method overloading provides more than one form for a method. Hence it is an example for polymorphism. Means a method with same name provide different functions.Suppose you want addition of two parameters and with same parameters subtraction then you can do like this...
int calculation(int x,int y)
{
return x + y;
}

int calculation(double x,int y)
{
return x - y;
}
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900