Re: Overloading and method signatures.
From: Bruce Wood (brucewood_at_canada.com)
Date: 03/05/05
- Next message: MS News \(taruntius\): "Re: Translating JavaScript function with Regex to CSharp"
- Previous message: Dave: "fixed"
- In reply to: Marcos Stefanakopolus: "Overloading and method signatures."
- Next in thread: Jon Skeet [C# MVP]: "Re: Overloading and method signatures."
- Reply: Jon Skeet [C# MVP]: "Re: Overloading and method signatures."
- Reply: Marcos Stefanakopolus: "Re: Overloading and method signatures."
- Messages sorted by: [ date ] [ thread ]
Date: 4 Mar 2005 21:48:45 -0800
The simple answer is because of precisely the example you gave: there
are many situations in which the compiler can't determine which one to
call.
The more complex answer involves looking at the solution you proposed,
and seeing it from the compiler's point of view. You suggested this bit
of code to disambiguate the situation:
class foo {
int myMethod(string) { ... }
double myMethod(string) { ... }
}
Decimal Num = 1.0 + (double)myMethod("Dave") + 7;
where the cast to (double) tells the compiler which method to call.
However, compilers don't see things that way.
As soon as you evaluate 'myMethod("Dave")', it becomes a value to be
used in a calculation. The compiler doesn't "look ahead," as such, to
see what kind of value it's going to need in the expression, or to what
kind of value this value is going to be cast. That's all "in the
future" as it were.
This isn't unique to C#, by the way. I know of no language that allows
overloads that will distinguish between two methods based on their
return values alone. In particular, C++ and Java, which provided
inspiration for C#, don't allow it, either.
- Next message: MS News \(taruntius\): "Re: Translating JavaScript function with Regex to CSharp"
- Previous message: Dave: "fixed"
- In reply to: Marcos Stefanakopolus: "Overloading and method signatures."
- Next in thread: Jon Skeet [C# MVP]: "Re: Overloading and method signatures."
- Reply: Jon Skeet [C# MVP]: "Re: Overloading and method signatures."
- Reply: Marcos Stefanakopolus: "Re: Overloading and method signatures."
- Messages sorted by: [ date ] [ thread ]
Relevant Pages
|