可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试):
问题:
This question already has an answer here:
-
Function overloading by return type?
14 answers
I am want to dig in that whether it is an ambiguity or an extra feature that is provided:
public class Foo
{
public int Bar(){
//code
}
public string Bar(int a){
//code
}
}
Any one having any experience with this, overloading on return type with different parameters should be a bad practice, is it?
But if the overloading was done on the basis of return type then why this is not working for.
public class Foo
{
public int Bar(int a){
//code
}
public string Bar(int a){
//code
}
}
As it will be unable to decide which function to call 1st or second, if we call obj.Bar(); , it should end in error do any one have any idea about it why it allows first code snippet to run.
回答1:
The C# specification (section 10.6) states that overloaded members may not differ by only return type and as per http://msdn.microsoft.com/en-us/library/ms229029.aspx
As per your question regarding creating parameters simply to support differing return types? I personally believe that is a terrible solution to the problem. Code maintenance will become difficult and unused parameters are a definite code smell. Does the method really need to be overloaded in that case? Or does it belong in that class? Should something else be created to convert from one return type to another? All things you should ask to derive a more idiomatic solution.
回答2:
This is logically impossible. Consider the following call:
object o = Bar(42);
or even
var o = Bar(42);
How would the compiler know which method to call?
Edit:
Now that I understand what you're actually asking, I think overloading by meaningless parameters is bad practice, and diminished readability, it is much preferable to distinguish by method name:
string BarToStr()
{
}
int BarToInt()
{
}
回答3:
Others already explained the situation. I only would like to add this: You can do what you have in mind by using a generic type parameter:
public T Bar<T>(int a) {
// code
}
And call it like this:
int i = Bar<int>(42);
string s = Bar<string>(42);
The problem is that it is often difficult to do something meaningful with a generic type, e.g. you cannot apply arithmetic operations to it. Sometimes generic type constraints can help.
回答4:
You can not overload the function by differing only their return type.
You can only overload the function in the following ways
- Parameter types
- Number of parameters
- Order of the parameters declared in the method
You can not come to know which function is actually called (if it was possible).
One more thing I would like to add is
function overloading is providing a function with the same name, but with a different signature. but the return type of a method is not considered as a part of the method's signature.
So this is another way to understand why method overloading can't be done only by return type.
回答5:
any one having any experience with this, overloading on return type
with different parameters should be a bad practice, is it.
I'm taking that to mean - "is it bad practice to use differing parameter combinations to facilitate different return types" - if that is indeed the question, then just imagine someone else coming across this code in a few months time - effectively "dummy" parameters to determine return type...it would be quite hard to understand what's going on.
Edit - as ChrisLava points out "the way around this is to have better names for each function (instead of overloading). Names that have clear meaning within the application. There is no reason why Bar should return an int and a string. I could see just calling ToString() on the int"
回答6:
C# does not allow it.
C# on the other hand does not support method resolution based on the
return type; this was a conscious decision by the language designers
not to expose the feature of the CLR which allows it. There's no
technical reason why they couldn't have done, they just felt it better
not to. Because the return value is not used for method selection a
C# method signature does not include it.
回答7:
Check this.. You can't have method with same signature and just different return type. Recommended is mentioned in this question.
C# Overload return type - recommended approach
回答8:
Your code will result in a Compiler Error. You can not have a Method of the same name with the same parameters and a different return type, the caller would not know which Method to call (will not be able to resolve the location in Memory of the Method to call, why the Compiler would not allow it). Your alternative would be to return an Object and Cast it based on what the Caller knows. Even then, that appears to be Bad design.
This will Work (meaning Compile), but still bad design.
public class Foo
{
public object Bar(int a)
{
return a;
}
}
回答9:
You can't create method with same name, same number and types of parameters.
For more details refer this link.