what is the use of over loaded functions.

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • veebhudhi.chandramouli@gmail.com

    #1

    what is the use of over loaded functions.

    Instead of using same name i can use different name,
    is there any specific purpous is there to define the polymorphic
    overloaded functions in c#

  • Mark Rae [MVP]

    #2
    Re: what is the use of over loaded functions.

    <veebhudhi.chan dramouli@gmail. comwrote in message
    news:1185796426 .667869.128470@ i38g2000prf.goo glegroups.com.. .
    Instead of using same name i can use different name,
    True, but then it's not the same function...:-)
    is there any specific purpous is there to define the polymorphic
    overloaded functions in c#
    It allows you to define different "versions" of the same function with
    different signatures - other languages did something similar with optional
    parameters, but C# doesn't, thank God!




    --
    Mark Rae
    ASP.NET MVP


    Comment

    • Jon Skeet [C# MVP]

      #3
      Re: what is the use of over loaded functions.

      On Jul 30, 12:53 pm, veebhudhi.chand ramo...@gmail.c om wrote:
      Instead of using same name i can use different name,
      is there any specific purpous is there to define the polymorphic
      overloaded functions in c#
      For constructors, you can't change the name to start with.
      For everything else - it's just more convenient not to have to create
      lots of different method names.

      Look at the overloads for Console.WriteLi ne, for example - would you
      really want that many different method names?

      Jon

      Comment

      • Hitesh Ashar

        #4
        Re: what is the use of over loaded functions.

        One use of overloaded is to provide default parameter values.

        Consider a function that takes three parameters with one parameter being
        the same for 90% of the calls.
        You could overload that function with one that takes two parameters and
        delegate the call to your original function providing the third value
        yourself.

        My 2c.

        veebhudhi.chand ramouli@gmail.c om wrote:
        Instead of using same name i can use different name,
        is there any specific purpous is there to define the polymorphic
        overloaded functions in c#
        >
        --
        Hitesh Ashar
        hitesh (at) asharism (dot) com

        Comment

        • Paul Werkowitz

          #5
          Re: what is the use of over loaded functions.

          Am Mon, 30 Jul 2007 13:02:26 +0100 schrieb Mark Rae [MVP]:
          >
          It allows you to define different "versions" of the same function with
          different signatures - other languages did something similar with optional
          parameters, but C# doesn't, thank God!
          >
          Optional parameters:
          Why do you prefer

          void f ( int data )
          void f ( int data, bool withLogging )
          over

          void f( int data, bool withLogging = false )

          ???

          Comment

          • Mark Rae [MVP]

            #6
            Re: what is the use of over loaded functions.

            "Paul Werkowitz" <newsgroups@pri maprogramm.dewr ote in message
            news:1tq0rqqgu7 n5q$.1pmlifo0vd 6il$.dlg@40tude .net...
            void f( int data, bool withLogging = false )
            That isn't an optional parameter - it's a parameter with a default value...

            Not the same thing at all!


            --
            Mark Rae
            ASP.NET MVP


            Comment

            • Jon Skeet [C# MVP]

              #7
              Re: what is the use of over loaded functions.

              On Jul 30, 2:19 pm, "Mark Rae [MVP]" <m...@markNOSPA Mrae.netwrote:
              void f( int data, bool withLogging = false )
              >
              That isn't an optional parameter - it's a parameter with a default value...
              >
              Not the same thing at all!
              I certainly see the two as being equivalent. There's no point in
              having a default value unless the parameter is optional, and if it's
              optional then surely it needs a default value.

              What's the practical difference between the two in your view?

              Jon

              Comment

              • Mark Rae [MVP]

                #8
                Re: what is the use of over loaded functions.

                "Jon Skeet [C# MVP]" <skeet@pobox.co mwrote in message
                news:1185802100 .956957.105850@ r34g2000hsd.goo glegroups.com.. .
                On Jul 30, 2:19 pm, "Mark Rae [MVP]" <m...@markNOSPA Mrae.netwrote:
                void f( int data, bool withLogging = false )
                >>
                >That isn't an optional parameter - it's a parameter with a default
                >value...
                >>
                >Not the same thing at all!
                >
                I certainly see the two as being equivalent. There's no point in
                having a default value unless the parameter is optional, and if it's
                optional then surely it needs a default value.
                >
                What's the practical difference between the two in your view?
                There's no practical difference, but an optional parameter (certainly in the
                VB utilisation) doesn't actually have to exist...


                --
                Mark Rae
                ASP.NET MVP


                Comment

                • Jon Skeet [C# MVP]

                  #9
                  Re: what is the use of over loaded functions.

                  On Jul 30, 3:42 pm, "Mark Rae [MVP]" <m...@markNOSPA Mrae.netwrote:
                  What's the practical difference between the two in your view?
                  >
                  There's no practical difference, but an optional parameter (certainly in the
                  VB utilisation) doesn't actually have to exist...
                  What exactly does that mean? What does a parameter which doesn't exist
                  look like?

                  Jon

                  Comment

                  • Mark Rae [MVP]

                    #10
                    Re: what is the use of over loaded functions.

                    "Jon Skeet [C# MVP]" <skeet@pobox.co mwrote in message
                    news:1185808485 .122284.163490@ 57g2000hsv.goog legroups.com...
                    On Jul 30, 3:42 pm, "Mark Rae [MVP]" <m...@markNOSPA Mrae.netwrote:
                    >>What's the practical difference between the two in your view?
                    >>
                    >There's no practical difference, but an optional parameter (certainly in
                    >the
                    >VB utilisation) doesn't actually have to exist...
                    >
                    What exactly does that mean? What does a parameter which doesn't exist
                    look like?
                    Private Sub Draw(Optional X As Variant, Optional Y As Variant)
                    If IsMissing(X) Then X = 720
                    If IsMissing(Y) Then Y = 2880
                    Cls
                    Circle (X, Y), 700
                    End Sub

                    Draw()
                    Draw(600)
                    Draw(600, 1500)


                    --
                    Mark Rae
                    ASP.NET MVP


                    Comment

                    • Jon Skeet [C# MVP]

                      #11
                      Re: what is the use of over loaded functions.

                      On Jul 30, 4:22 pm, "Mark Rae [MVP]" <m...@markNOSPA Mrae.netwrote:
                      What exactly does that mean? What does a parameter which doesn't exist
                      look like?
                      >
                      Private Sub Draw(Optional X As Variant, Optional Y As Variant)
                      If IsMissing(X) Then X = 720
                      If IsMissing(Y) Then Y = 2880
                      Cls
                      Circle (X, Y), 700
                      End Sub
                      >
                      Draw()
                      Draw(600)
                      Draw(600, 1500)
                      And what does that compile to in IL? Is "IsMissing" effectively the
                      same as "IsNothing" or whatever the VB terminology is? Does it work
                      for types other than Variant?

                      Jon

                      Comment

                      • Mark Rae [MVP]

                        #12
                        Re: what is the use of over loaded functions.

                        "Jon Skeet [C# MVP]" <skeet@pobox.co mwrote in message
                        news:1185809806 .767873.202510@ d55g2000hsg.goo glegroups.com.. .
                        And what does that compile to in IL? Is "IsMissing" effectively the
                        same as "IsNothing" or whatever the VB terminology is?
                        I have no idea - that's actually an old bit of VB code - no idea at all if
                        it would work in VB.NET... :-)
                        Does it work for types other than Variant?
                        It didn't in VB...


                        --
                        Mark Rae
                        ASP.NET MVP


                        Comment

                        • Jon Skeet [C# MVP]

                          #13
                          Re: what is the use of over loaded functions.

                          On Jul 30, 4:42 pm, "Mark Rae [MVP]" <m...@markNOSPA Mrae.netwrote:
                          And what does that compile to in IL? Is "IsMissing" effectively the
                          same as "IsNothing" or whatever the VB terminology is?
                          >
                          I have no idea - that's actually an old bit of VB code - no idea at all if
                          it would work in VB.NET... :-)
                          Ah. Looking in MSDN for VB.NET, it specifies:

                          <quote>
                          Every optional parameter in the procedure definition must specify a
                          default value.
                          </quote>

                          So at least in VB.NET, they *are* the same thing after all. That's a
                          relief in terms of my mental model, at the very least :)
                          Does it work for types other than Variant?
                          >
                          It didn't in VB...
                          I suspect that it was just a "default default" value which was the
                          equivalent of Nothing, or some singleton "missing value".

                          Jon

                          Comment

                          • G.Doten

                            #14
                            Re: what is the use of over loaded functions.

                            Jon Skeet [C# MVP] wrote:
                            On Jul 30, 4:42 pm, "Mark Rae [MVP]" <m...@markNOSPA Mrae.netwrote:
                            >>And what does that compile to in IL? Is "IsMissing" effectively the
                            >>same as "IsNothing" or whatever the VB terminology is?
                            >I have no idea - that's actually an old bit of VB code - no idea at all if
                            >it would work in VB.NET... :-)
                            >
                            Ah. Looking in MSDN for VB.NET, it specifies:
                            >
                            <quote>
                            Every optional parameter in the procedure definition must specify a
                            default value.
                            </quote>
                            Yes, in fact "optional parameter" is a misleading term. I remember a
                            long thread back around 2000 in this ng, back when C# was new and some
                            people had a real issue with C# not having VB's concept of a so-called
                            "optional parameter" (and, indeed, VB.NET not supporting optional
                            parameters a la VB6). They aren't optional parameters but are hard-coded
                            default values.

                            IIRC, they are not fun to use because they are like a C# public const
                            variable vs. a readonly variable. With the former, the const value is
                            actually pulled out of the referenced assembly and inserted directly
                            into the consuming assembly; with the latter, a reference to the
                            readonly variable is deposited into the consuming assembly. Why is this
                            important. Well if you set your const to 12 and then change it to 13
                            then each and every assembly that references that public const must then
                            be recompiled to get the new 13 value. VB.NET's so-called optional
                            parameters work the same way. Avoid them! :-)

                            Here's what I could find with Google.

                            Real optional params (i.e., much of what has been discussed again in
                            this thread and real optional parameters with C#'s params keyword):



                            Pointed to from a thread in this ng - calling COM objects that have
                            optional parameters:



                            And I joined in the fray here:



                            Lastly, another thread I just read points out that VB.NET optional
                            parameters are not CLS-compliant so, for example, an optional parameter
                            to a VB.NET method would only be optional to other VB.NET code, to C#
                            and other non-VB.NET languages the parameter would not be optional.

                            HTH.


                            --
                            -glenn-

                            Comment

                            • Mark Rae [MVP]

                              #15
                              Re: what is the use of over loaded functions.

                              "Jon Skeet [C# MVP]" <skeet@pobox.co mwrote in message
                              news:1185810806 .017311.22110@w 3g2000hsg.googl egroups.com...
                              And what does that compile to in IL? Is "IsMissing" effectively the
                              same as "IsNothing" or whatever the VB terminology is?
                              >>
                              >I have no idea - that's actually an old bit of VB code - no idea at all
                              >if
                              >it would work in VB.NET... :-)
                              >
                              Ah. Looking in MSDN for VB.NET, it specifies:
                              >
                              <quote>
                              Every optional parameter in the procedure definition must specify a
                              default value.
                              </quote>
                              >
                              So at least in VB.NET, they *are* the same thing after all. That's a
                              relief in terms of my mental model, at the very least :)
                              I couldn't agree more! I always hated that whole VB "sloppiness "...
                              Does it work for types other than Variant?
                              >>
                              >It didn't in VB...
                              >
                              I suspect that it was just a "default default" value which was the
                              equivalent of Nothing, or some singleton "missing value".
                              No doubt.


                              --
                              Mark Rae
                              ASP.NET MVP


                              Comment

                              Working...