An overload is based on a methods signature, which is its name and its
parameters. When you call a function like:
MyFunction("Foo");
and then like:
MyFunction("Bar");
The compiler must make a distinction between the two in order to determine
which overload to call. In this case, the name and parameters are the same,
so the same method would be called.
The assignment, like the following
int x = MyFunction("AddNumbers");
occurs after the method call has been chosen. At this point, the compiler is
matching the return type of the method it chose based on the methods
signature and parameteres passed with the type the value is being assigned
to.
While you could argue that the compiler could see that you have 10 different
functions that take strings of the same name, and it could look to see what
type of return value you were looking for, this would remove the type safety
you are given in C#. This works fine for an untyped language where the type
does not matter. But why then would you care about the return type?
Hope this helps...
Frisky
"Zeeshan Shakeel" <Zeeshan
Sh*****@discussions.microsoft.com> wrote in
message news:F0**********************************@microsof t.com...
Well , I don't agree with your answer, primary reason is that when u
design
a compiler then it is upto u , u can definitely overload on the basis of
return type as well using attibutes of "Attributes Context Free Grammer "
.
But this probably requires an overhead & thus it increases complexity of
compiler. That 's why compiler writer don't normally prefer doing it.
I will appreciate any comments on it.
"Michael C" wrote:
eg
void DoIt()
{
int i = FromString("1");
double d = FromString("1.1");
}
int FromString(string SomeValue)
{
return int.Parse(SomeValue);
}
double FromString(string SomeValue)
{
return double.Parse(SomeValue);
}
The return type is really just a byref param underneath anyway I presume.