Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. Overloading

Overloading

Scheduled Pinned Locked Moved C#
csharpc++htmlcomquestion
8 Posts 4 Posters 1 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • G Offline
    G Offline
    Gary Kirkham
    wrote on last edited by
    #1

    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

    D H C 3 Replies Last reply
    0
    • G Gary Kirkham

      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

      H Offline
      H Offline
      Heath Stewart
      wrote on last edited by
      #2

      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

      G 1 Reply Last reply
      0
      • G Gary Kirkham

        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

        D Offline
        D Offline
        Dave Kreskowiak
        wrote on last edited by
        #3

        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

        1 Reply Last reply
        0
        • H Heath Stewart

          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

          G Offline
          G Offline
          Gary Kirkham
          wrote on last edited by
          #4

          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

          H 1 Reply Last reply
          0
          • G Gary Kirkham

            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

            H Offline
            H Offline
            Heath Stewart
            wrote on last edited by
            #5

            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

            1 Reply Last reply
            0
            • G Gary Kirkham

              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

              C Offline
              C Offline
              Curi0us_George
              wrote on last edited by
              #6

              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.

              G 1 Reply Last reply
              0
              • C Curi0us_George

                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.

                G Offline
                G Offline
                Gary Kirkham
                wrote on last edited by
                #7

                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

                C 1 Reply Last reply
                0
                • G Gary Kirkham

                  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

                  C Offline
                  C Offline
                  Curi0us_George
                  wrote on last edited by
                  #8

                  Not a big deal. I just couldn't have you bad-mouthing C#'s overloading. ;)

                  1 Reply Last reply
                  0
                  Reply
                  • Reply as topic
                  Log in to reply
                  • Oldest to Newest
                  • Newest to Oldest
                  • Most Votes


                  • Login

                  • Don't have an account? Register

                  • Login or register to search.
                  • First post
                    Last post
                  0
                  • Categories
                  • Recent
                  • Tags
                  • Popular
                  • World
                  • Users
                  • Groups