M
Marcos Stefanakopolus
So this is ok:
class foo {
int myMethod(string, int) { ... }
int myMethod(string, double) { ... }
}
But this is not:
class foo {
int myMethod(string) { ... }
double myMethod(string) { ... }
}
The compiler complains "Class 'foo' already defines a member called
'myMethod' with the same parameter types'. Well, yes that's true, but my
question is "why on earth is the return type not considered part of a
method's signature for purposes of overload disambiguation?"
Can someone with perhaps a deeper understanding of C# clue me in as to why
this is the case? When calling the method in contexts such as:
int I = myMethod("Bob");
double D = myMethod("Sue");
There's no ambiguity about which implementation is meant. In contexts such
as:
Decimal Num = 1.0 + myMethod("Dave") + 7;
The compiler could simply require a cast to disambiguiate which type is
intended. The C# compiler doesn't seem to have any problem requiring silly
casts in all sorts of other places, so why would it be so bad to require
this:
Decimal Num = 1.0 + (double)myMethod("Dave") + 7;
Perhaps I'm just irritated that C# is making my job more difficult at the
moment because return type is _not_ part of a method's signature, but I'd
like to understand why and hear any suggestions as to ways to handle
situations where you do want to have two methods with the same name that
differ only in return type.
class foo {
int myMethod(string, int) { ... }
int myMethod(string, double) { ... }
}
But this is not:
class foo {
int myMethod(string) { ... }
double myMethod(string) { ... }
}
The compiler complains "Class 'foo' already defines a member called
'myMethod' with the same parameter types'. Well, yes that's true, but my
question is "why on earth is the return type not considered part of a
method's signature for purposes of overload disambiguation?"
Can someone with perhaps a deeper understanding of C# clue me in as to why
this is the case? When calling the method in contexts such as:
int I = myMethod("Bob");
double D = myMethod("Sue");
There's no ambiguity about which implementation is meant. In contexts such
as:
Decimal Num = 1.0 + myMethod("Dave") + 7;
The compiler could simply require a cast to disambiguiate which type is
intended. The C# compiler doesn't seem to have any problem requiring silly
casts in all sorts of other places, so why would it be so bad to require
this:
Decimal Num = 1.0 + (double)myMethod("Dave") + 7;
Perhaps I'm just irritated that C# is making my job more difficult at the
moment because return type is _not_ part of a method's signature, but I'd
like to understand why and hear any suggestions as to ways to handle
situations where you do want to have two methods with the same name that
differ only in return type.