Overloading
-
Why can't I have two functions with the same name and the same input paramters, but different return types. The two functions are obviously different. I did this in C++ with no problems. Is there any way to do it in c#? Gary Kirkham A working Program is one that has only unobserved bugs He is no fool who gives what he cannot keep to gain what he cannot lose. - Jim Elliot Who you are in Christ
-
Why can't I have two functions with the same name and the same input paramters, but different return types. The two functions are obviously different. I did this in C++ with no problems. Is there any way to do it in c#? Gary Kirkham A working Program is one that has only unobserved bugs He is no fool who gives what he cannot keep to gain what he cannot lose. - Jim Elliot Who you are in Christ
It's not allowed by the CLI. See ECMA 335, section 10.2 (Overloading), with one exception: overloaded operator methods can differ only by return type.
Microsoft MVP, Visual C# My Articles
-
Why can't I have two functions with the same name and the same input paramters, but different return types. The two functions are obviously different. I did this in C++ with no problems. Is there any way to do it in c#? Gary Kirkham A working Program is one that has only unobserved bugs He is no fool who gives what he cannot keep to gain what he cannot lose. - Jim Elliot Who you are in Christ
You can't because C# doesn't support it. Overloading selections are done only on functions names and argument lists only. Return types are not considered by the compiler. 7.4.2 Overload Resolution[^] RageInTheMachine9532 "...a pungent, gastly, stinky piece of cheese!" -- The Roaming Gnome
-
It's not allowed by the CLI. See ECMA 335, section 10.2 (Overloading), with one exception: overloaded operator methods can differ only by return type.
Microsoft MVP, Visual C# My Articles
Thanks...I wonder why the C# development team thought that capability wasn't useful? O' Well, maybe I am the only one who used it in C++. :) Gary Kirkham A working Program is one that has only unobserved bugs He is no fool who gives what he cannot keep to gain what he cannot lose. - Jim Elliot Who you are in Christ
-
Thanks...I wonder why the C# development team thought that capability wasn't useful? O' Well, maybe I am the only one who used it in C++. :) Gary Kirkham A working Program is one that has only unobserved bugs He is no fool who gives what he cannot keep to gain what he cannot lose. - Jim Elliot Who you are in Christ
It's not C#: it's the CLI - the Common Language Infrastructure (sometimes Interface), which describes assembly and type metadata (Common Type System, or CTS), as well as the Common Intermediate Language (CIL) and a few other things. It's what makes the language independence possible.
Microsoft MVP, Visual C# My Articles
-
Why can't I have two functions with the same name and the same input paramters, but different return types. The two functions are obviously different. I did this in C++ with no problems. Is there any way to do it in c#? Gary Kirkham A working Program is one that has only unobserved bugs He is no fool who gives what he cannot keep to gain what he cannot lose. - Jim Elliot Who you are in Christ
Since no one else said it, I guess I will: You did NOT do this in C++. class A { int doSomething() {} void doSomething() {} // causes an error }; Here's the first error it will cause: error C2556: 'void A::doSomething(void)' : overloaded function differs only by return type from 'int A::doSomething(void)' It will also cause other errors indirectly, when/if the compiler fails to recover from the error and continue analysis. The C++ standards forbid this, just as C# does.
-
Since no one else said it, I guess I will: You did NOT do this in C++. class A { int doSomething() {} void doSomething() {} // causes an error }; Here's the first error it will cause: error C2556: 'void A::doSomething(void)' : overloaded function differs only by return type from 'int A::doSomething(void)' It will also cause other errors indirectly, when/if the compiler fails to recover from the error and continue analysis. The C++ standards forbid this, just as C# does.
I stand corrected, I must be getting senile in my old age.:) Gary Kirkham A working Program is one that has only unobserved bugs He is no fool who gives what he cannot keep to gain what he cannot lose. - Jim Elliot Who you are in Christ
-
I stand corrected, I must be getting senile in my old age.:) Gary Kirkham A working Program is one that has only unobserved bugs He is no fool who gives what he cannot keep to gain what he cannot lose. - Jim Elliot Who you are in Christ
Not a big deal. I just couldn't have you bad-mouthing C#'s overloading. ;)