method overloading and references

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Daniel Gustafsson

    #1

    method overloading and references

    Hi there, hmm, I've just started with C# and I'm experimenting with method
    overloading.
    It seems like it's not possible to override method using return types, only
    parameters. Is that by design, anyway around that or something? I.e. I'd
    like to have the same method return either a DataReader or a DataAdapter
    depending on the lvalue when calling it.

    And another question. If I have my method returning a value, can it return
    it by reference or does it always, always copy the object?

    Anyhow, it all boils down to the fact that my class will have methods that
    are supposed to retrieve database results for me, and I want them to be able
    to provide me both with DataAdapters (for easy integration with components)
    and DataReaders (in the case I want to use the result in a loop instead of a
    component). I want to do this as seemless and simple as possible without
    having any duplication of code, 3x as many methods or stuff like that AND
    the fact that it has to be optimal performance-wize.


  • Jon Skeet

    #2
    Re: method overloading and references

    Daniel Gustafsson <daniel@phoswor ks.se> wrote:[color=blue]
    > Hi there, hmm, I've just started with C# and I'm experimenting with method
    > overloading.
    > It seems like it's not possible to override method using return types, only
    > parameters. Is that by design, anyway around that or something?[/color]

    It's by design - how would you be able to distinguish them at the
    client side? For instance:

    MyMethod();

    could return a value, but I don't *have* to use it - which version
    would that call?
    [color=blue]
    > I.e. I'd
    > like to have the same method return either a DataReader or a DataAdapter
    > depending on the lvalue when calling it.[/color]

    That strikes me as a fairly confusing interface, to be honest.
    [color=blue]
    > And another question. If I have my method returning a value, can it return
    > it by reference or does it always, always copy the object?[/color]

    It doesn't "return it by reference" - it returns *a* reference. You
    need to understand exactly what the difference between reference types
    and value types are.

    If you want to make a copy of the object, you should probably do that
    in the method, and return a reference to the copy.
    [color=blue]
    > Anyhow, it all boils down to the fact that my class will have methods that
    > are supposed to retrieve database results for me, and I want them to be able
    > to provide me both with DataAdapters (for easy integration with components)
    > and DataReaders (in the case I want to use the result in a loop instead of a
    > component). I want to do this as seemless and simple as possible without
    > having any duplication of code, 3x as many methods or stuff like that AND
    > the fact that it has to be optimal performance-wize.[/color]

    Well, you could provide another parameter to say what type of reference
    to return, and declare it as returning "object", leaving it to the
    caller to cast it. That doesn't seem as nice a way as having a strongly
    typed return value though. I'd suggest either having different API
    classes, or *possibly* (and I don't like this particularly either) have
    two interfaces, one of which describes returning a DataReader and one
    of which describes returning a DataAdapter. Then use explicit interface
    implementation to tell the difference between the two, and let the
    client cast their initial reference to one of the two interfaces
    depending on what they want to do.

    --
    Jon Skeet - <skeet@pobox.co m>
    Pobox has been discontinued as a separate service, and all existing customers moved to the Fastmail platform.

    If replying to the group, please do not mail me too

    Comment

    • Jon Skeet

      #3
      Re: method overloading and references

      Daniel Gustafsson <daniel@phoswor ks.se> wrote:[color=blue]
      > Hi there, hmm, I've just started with C# and I'm experimenting with method
      > overloading.
      > It seems like it's not possible to override method using return types, only
      > parameters. Is that by design, anyway around that or something?[/color]

      It's by design - how would you be able to distinguish them at the
      client side? For instance:

      MyMethod();

      could return a value, but I don't *have* to use it - which version
      would that call?
      [color=blue]
      > I.e. I'd
      > like to have the same method return either a DataReader or a DataAdapter
      > depending on the lvalue when calling it.[/color]

      That strikes me as a fairly confusing interface, to be honest.
      [color=blue]
      > And another question. If I have my method returning a value, can it return
      > it by reference or does it always, always copy the object?[/color]

      It doesn't "return it by reference" - it returns *a* reference. You
      need to understand exactly what the difference between reference types
      and value types are.

      If you want to make a copy of the object, you should probably do that
      in the method, and return a reference to the copy.
      [color=blue]
      > Anyhow, it all boils down to the fact that my class will have methods that
      > are supposed to retrieve database results for me, and I want them to be able
      > to provide me both with DataAdapters (for easy integration with components)
      > and DataReaders (in the case I want to use the result in a loop instead of a
      > component). I want to do this as seemless and simple as possible without
      > having any duplication of code, 3x as many methods or stuff like that AND
      > the fact that it has to be optimal performance-wize.[/color]

      Well, you could provide another parameter to say what type of reference
      to return, and declare it as returning "object", leaving it to the
      caller to cast it. That doesn't seem as nice a way as having a strongly
      typed return value though. I'd suggest either having different API
      classes, or *possibly* (and I don't like this particularly either) have
      two interfaces, one of which describes returning a DataReader and one
      of which describes returning a DataAdapter. Then use explicit interface
      implementation to tell the difference between the two, and let the
      client cast their initial reference to one of the two interfaces
      depending on what they want to do.

      --
      Jon Skeet - <skeet@pobox.co m>
      Pobox has been discontinued as a separate service, and all existing customers moved to the Fastmail platform.

      If replying to the group, please do not mail me too

      Comment

      • Christian

        #4
        Re: method overloading and references

        > It seems like it's not possible to override method using return types,
        only
        Override methods must have same signature, that means even return type
        Did you intend overload?
        [color=blue]
        > I.e. I'd like to have the same method return either a DataReader or a[/color]
        DataAdapter[color=blue]
        > depending on the lvalue when calling it.[/color]
        lvalue?? Left Value?
        public Class A
        {
        public DataReader YourMethod()
        {
        ...
        }
        public DataAdatpter YourMethod()
        {
        ...
        }
        }
        DataReader dr = YourMethod( ); // This way???
        Left Value is optional so how can we distinguish 2 methods?

        [color=blue]
        > And another question. If I have my method returning a value, can it return
        > it by reference or does it always, always copy the object?[/color]
        if return type is a reference type it is implicit a reference, not a copy


        Comment

        • Christian

          #5
          Re: method overloading and references

          > It seems like it's not possible to override method using return types,
          only
          Override methods must have same signature, that means even return type
          Did you intend overload?
          [color=blue]
          > I.e. I'd like to have the same method return either a DataReader or a[/color]
          DataAdapter[color=blue]
          > depending on the lvalue when calling it.[/color]
          lvalue?? Left Value?
          public Class A
          {
          public DataReader YourMethod()
          {
          ...
          }
          public DataAdatpter YourMethod()
          {
          ...
          }
          }
          DataReader dr = YourMethod( ); // This way???
          Left Value is optional so how can we distinguish 2 methods?

          [color=blue]
          > And another question. If I have my method returning a value, can it return
          > it by reference or does it always, always copy the object?[/color]
          if return type is a reference type it is implicit a reference, not a copy


          Comment

          • Daniel Gustafsson

            #6
            Re: method overloading and references


            "Christian" <pleasenononosp amcmillotti@nov asoftware.it> wrote in message
            news:O3uRBeBWDH A.452@TK2MSFTNG P10.phx.gbl...[color=blue]
            > DataReader dr = YourMethod( ); // This way???
            > Left Value is optional so how can we distinguish 2 methods?[/color]
            Yeah, I figured that was the case why it didn't work.

            [color=blue][color=green]
            > > And another question. If I have my method returning a value, can it[/color][/color]
            return[color=blue][color=green]
            > > it by reference or does it always, always copy the object?[/color]
            > if return type is a reference type it is implicit a reference, not a copy[/color]

            So I don't have to worry about returning DataAdapters/DataReaders will give
            me a performance hit compared to having an out-parameter?


            Comment

            • Daniel Gustafsson

              #7
              Re: method overloading and references


              "Christian" <pleasenononosp amcmillotti@nov asoftware.it> wrote in message
              news:O3uRBeBWDH A.452@TK2MSFTNG P10.phx.gbl...[color=blue]
              > DataReader dr = YourMethod( ); // This way???
              > Left Value is optional so how can we distinguish 2 methods?[/color]
              Yeah, I figured that was the case why it didn't work.

              [color=blue][color=green]
              > > And another question. If I have my method returning a value, can it[/color][/color]
              return[color=blue][color=green]
              > > it by reference or does it always, always copy the object?[/color]
              > if return type is a reference type it is implicit a reference, not a copy[/color]

              So I don't have to worry about returning DataAdapters/DataReaders will give
              me a performance hit compared to having an out-parameter?


              Comment

              • Christian

                #8
                Re: method overloading and references


                [color=blue]
                > So I don't have to worry about returning DataAdapters/DataReaders will[/color]
                give[color=blue]
                > me a performance hit compared to having an out-parameter?
                >
                >[/color]
                No, returning a reference type shoul mean return 4 bytes indicating where
                the object is located in the HEAP


                Comment

                • Christian

                  #9
                  Re: method overloading and references


                  [color=blue]
                  > So I don't have to worry about returning DataAdapters/DataReaders will[/color]
                  give[color=blue]
                  > me a performance hit compared to having an out-parameter?
                  >
                  >[/color]
                  No, returning a reference type shoul mean return 4 bytes indicating where
                  the object is located in the HEAP


                  Comment

                  • Daniel Gustafsson

                    #10
                    Re: method overloading and references


                    "Christian" <pleasenononosp amcmillotti@nov asoftware.it> wrote in message
                    news:uN2yivBWDH A.2352@TK2MSFTN GP12.phx.gbl...[color=blue]
                    >
                    >[color=green]
                    > > So I don't have to worry about returning DataAdapters/DataReaders will[/color]
                    > give[color=green]
                    > > me a performance hit compared to having an out-parameter?
                    > >
                    > >[/color]
                    > No, returning a reference type shoul mean return 4 bytes indicating where
                    > the object is located in the HEAP
                    >
                    >[/color]

                    Ok, but afaik structures are value types, there is no way to return a
                    reference rather than getting it copied? the "ref" keyword only works for
                    parameters afaik. in c++ I can easily , and transparently, have my
                    functions return a reference to a value type.


                    Comment

                    • Daniel Gustafsson

                      #11
                      Re: method overloading and references


                      "Christian" <pleasenononosp amcmillotti@nov asoftware.it> wrote in message
                      news:uN2yivBWDH A.2352@TK2MSFTN GP12.phx.gbl...[color=blue]
                      >
                      >[color=green]
                      > > So I don't have to worry about returning DataAdapters/DataReaders will[/color]
                      > give[color=green]
                      > > me a performance hit compared to having an out-parameter?
                      > >
                      > >[/color]
                      > No, returning a reference type shoul mean return 4 bytes indicating where
                      > the object is located in the HEAP
                      >
                      >[/color]

                      Ok, but afaik structures are value types, there is no way to return a
                      reference rather than getting it copied? the "ref" keyword only works for
                      parameters afaik. in c++ I can easily , and transparently, have my
                      functions return a reference to a value type.


                      Comment

                      • Christian

                        #12
                        Re: method overloading and references

                        As struct are value types if you want a method return a reference to it you
                        should box it in an object,
                        but which are the reason for doing such an odd thing?
                        "Daniel Gustafsson" <daniel@phoswor ks.se> ha scritto nel messaggio
                        news:e8cCi4BWDH A.2476@tk2msftn gp13.phx.gbl...[color=blue]
                        >
                        > "Christian" <pleasenononosp amcmillotti@nov asoftware.it> wrote in message
                        > news:uN2yivBWDH A.2352@TK2MSFTN GP12.phx.gbl...[color=green]
                        > >
                        > >[color=darkred]
                        > > > So I don't have to worry about returning DataAdapters/DataReaders will[/color]
                        > > give[color=darkred]
                        > > > me a performance hit compared to having an out-parameter?
                        > > >
                        > > >[/color]
                        > > No, returning a reference type shoul mean return 4 bytes indicating[/color][/color]
                        where[color=blue][color=green]
                        > > the object is located in the HEAP
                        > >
                        > >[/color]
                        >
                        > Ok, but afaik structures are value types, there is no way to return a
                        > reference rather than getting it copied? the "ref" keyword only works for
                        > parameters afaik. in c++ I can easily , and transparently, have my
                        > functions return a reference to a value type.
                        >
                        >[/color]


                        Comment

                        • Christian

                          #13
                          Re: method overloading and references

                          As struct are value types if you want a method return a reference to it you
                          should box it in an object,
                          but which are the reason for doing such an odd thing?
                          "Daniel Gustafsson" <daniel@phoswor ks.se> ha scritto nel messaggio
                          news:e8cCi4BWDH A.2476@tk2msftn gp13.phx.gbl...[color=blue]
                          >
                          > "Christian" <pleasenononosp amcmillotti@nov asoftware.it> wrote in message
                          > news:uN2yivBWDH A.2352@TK2MSFTN GP12.phx.gbl...[color=green]
                          > >
                          > >[color=darkred]
                          > > > So I don't have to worry about returning DataAdapters/DataReaders will[/color]
                          > > give[color=darkred]
                          > > > me a performance hit compared to having an out-parameter?
                          > > >
                          > > >[/color]
                          > > No, returning a reference type shoul mean return 4 bytes indicating[/color][/color]
                          where[color=blue][color=green]
                          > > the object is located in the HEAP
                          > >
                          > >[/color]
                          >
                          > Ok, but afaik structures are value types, there is no way to return a
                          > reference rather than getting it copied? the "ref" keyword only works for
                          > parameters afaik. in c++ I can easily , and transparently, have my
                          > functions return a reference to a value type.
                          >
                          >[/color]


                          Comment

                          • Daniel Gustafsson

                            #14
                            Re: method overloading and references


                            "Christian" <pleasenononosp amcmillotti@nov asoftware.it> wrote in message
                            news:OzEf9ECWDH A.2340@TK2MSFTN GP10.phx.gbl...[color=blue]
                            > As struct are value types if you want a method return a reference to it[/color]
                            you[color=blue]
                            > should box it in an object,
                            > but which are the reason for doing such an odd thing?[/color]

                            Well, I didn't mean DataAdapters earlier on but I meant DataSets (for
                            components) and I've explained why I wanted to be able to fetch both types
                            as simplistic as possible.

                            And the returning structs -question was just out of curiousity since I did
                            some experimentation and it didn't seem like I could return references the
                            way I can in c++. I'm trying to figure all this out in a really short
                            timespan.


                            Comment

                            • Daniel Gustafsson

                              #15
                              Re: method overloading and references


                              "Christian" <pleasenononosp amcmillotti@nov asoftware.it> wrote in message
                              news:OzEf9ECWDH A.2340@TK2MSFTN GP10.phx.gbl...[color=blue]
                              > As struct are value types if you want a method return a reference to it[/color]
                              you[color=blue]
                              > should box it in an object,
                              > but which are the reason for doing such an odd thing?[/color]

                              Well, I didn't mean DataAdapters earlier on but I meant DataSets (for
                              components) and I've explained why I wanted to be able to fetch both types
                              as simplistic as possible.

                              And the returning structs -question was just out of curiousity since I did
                              some experimentation and it didn't seem like I could return references the
                              way I can in c++. I'm trying to figure all this out in a really short
                              timespan.


                              Comment

                              Working...