Once you change the return type, and keep the same function name, it will not compile.
here's a blog post which might clarify things a little.
//this will not work
public int WrapNumber(int i)
{
return i + 0;
}
public string WrapNumber(int i, int j)
{
return (i + j).ToString();
}
//this would work
public string WrapNumber(int i)
{
return (i + 0).ToString();
}
public string WrapNumber(int i, int j)
{
return (i + j).ToString();
}
Remember overloading is creating different methods by the same name with different signatures.
The signature however does not include the return type.
So you can have intWrapNumber(int i)and stringWrapNumber(int i,int j)because the signatures are different.
But you cannot then have say double
WrapNumber(int i)
since that will clutch with the first signature. (same name, same input parameters. return type does not count)
Yes, it is OK to have different return types. The signatures must be different and the signature, explicitly, does not include the return type. C# Language Specification section 3.6
The signature of a method consists of the name of the method, the number of type parameters and the type and kind (value, reference, or output) of each of its formal parameters, considered in the order left to right. For these purposes, any type parameter
of the method that occurs in the type of a formal parameter is identified not by its name, but by its ordinal position in the type argument list of the method. The signature of a method specifically does not include the return type, the params modifier that
may be specified for the right-most parameter, nor the optional type parameter constraints.
So your example is OK because the two methods have different signatures.
However, it is a bad idea to have overloaded methods with different return types. If two methods have different return types then they are doing different actions and should be given different names. The universal expectation is that methods with
the same name do a similar operation. Having different return types breaks that expectation and will lead to tears before bedtime.
chiragvidani
Participant
1288 Points
254 Posts
Function overloading based on different return types.
Apr 16, 2012 12:11 PM|LINK
Hello,
Can we achieve function overloading with different type and number of arguments as well as different return type too?
public int WrapNumber(int i) { return i + 0; } public string WrapNumber(int i, int j) { return (i + j).ToString(); }Please help me as I am little bit confused regarding this...
Chirag Vidani | My Blog
MCTS - .Net Framework 4.0, Web Applications
"…Mark As Answer" if my reply is helpful to you…”
karthicks
All-Star
31378 Points
5422 Posts
Re: Function overloading based on different return types.
Apr 16, 2012 12:19 PM|LINK
hi,
overloading can be done only by differentiating the parameter types and no of parameters not by return type
Refer : http://www.c-sharpcorner.com/uploadfile/jitendra1987/method-overloading-in-C-Sharp/
Karthick S
formationusa
Participant
1290 Points
215 Posts
Re: Function overloading based on different return types.
Apr 16, 2012 12:21 PM|LINK
Hello chiragvidani,
Method overloading only concerns parameters.
Once you change the return type, and keep the same function name, it will not compile.
here's a blog post which might clarify things a little.
//this will not work public int WrapNumber(int i) { return i + 0; } public string WrapNumber(int i, int j) { return (i + j).ToString(); } //this would work public string WrapNumber(int i) { return (i + 0).ToString(); } public string WrapNumber(int i, int j) { return (i + j).ToString(); }hope this helps.
http://oudinia.blogspot.com
srikar1
Member
82 Points
23 Posts
Re: Function overloading based on different return types.
Apr 16, 2012 12:22 PM|LINK
We can achieve function overloading with different types, number of arguments and order of the arguments. But not with different return types.
chiragvidani
Participant
1288 Points
254 Posts
Re: Function overloading based on different return types.
Apr 16, 2012 02:11 PM|LINK
Hello,
But the Visual Studio IDE says something like this
Chirag Vidani | My Blog
MCTS - .Net Framework 4.0, Web Applications
"…Mark As Answer" if my reply is helpful to you…”
krokonoster
Contributor
4291 Points
1352 Posts
Re: Function overloading based on different return types.
Apr 16, 2012 03:01 PM|LINK
Remember overloading is creating different methods by the same name with different signatures.
The signature however does not include the return type.
So you can have int WrapNumber(int i) and string WrapNumber(int i, int j) because the signatures are different.
But you cannot then have say double WrapNumber(int i) since that will clutch with the first signature. (same name, same input parameters. return type does not count)
Not sure if that make sense?
Paul Linton
Star
13421 Points
2535 Posts
Re: Function overloading based on different return types.
Apr 16, 2012 10:59 PM|LINK
Yes, it is OK to have different return types. The signatures must be different and the signature, explicitly, does not include the return type. C# Language Specification section 3.6
So your example is OK because the two methods have different signatures.
However, it is a bad idea to have overloaded methods with different return types. If two methods have different return types then they are doing different actions and should be given different names. The universal expectation is that methods with the same name do a similar operation. Having different return types breaks that expectation and will lead to tears before bedtime.