Difference between 'out' and 'ref' parameters.

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Chris Mayers

    #1

    Difference between 'out' and 'ref' parameters.

    'out' and 'ref' parameters in C#...

    Both these can be used to pass parameter values BACK from a Method, but
    obviously they are different techniques.

    As I understand it,

    'ref' parameter passes the object into the Method by Reference, ie passes
    the memory location of the original object in the parameter list. The Method
    may change the value of the object and by doing so, will be changing the
    value of the original object.

    'out' parameter passes an object BY VALUE back into the object in the
    calling parameters.

    1) Am I right?
    2) Is it true to say that you are usually better to use 'out' unless the
    original value of the parameter being passed in effects its value when you
    pass it back. Or if your method needs to return more than one calculated
    result.

    eg

    private void GetPosition(str ing lorryId, out int xLocation, out int
    yLocation)

    Hope this makes sense, I'm very tired...

    Thanks,

    Chris.


  • LOZANO-MORÁN, Gabriel

    #2
    Re: Difference between 'out' and 'ref' parameters.

    Both by reference, difference is that if you use an out parameter you don't
    need to initialize that parameter variable. So if you want to do the
    initialization in a method you can use a out parameter.

    Ref (initialize the variable)
    int getal = 0;
    RefTest(ref getal);

    Out (no need to intialize the variable)
    int getal;
    OutTest(out getal);

    Gabriel Lozano-Morán

    "Chris Mayers" <chris_mayersBL UE@SUEDEYahoo.C om> wrote in message
    news:#4cRWRBRFH A.3076@TK2MSFTN GP14.phx.gbl...[color=blue]
    > 'out' and 'ref' parameters in C#...
    >
    > Both these can be used to pass parameter values BACK from a Method, but
    > obviously they are different techniques.
    >
    > As I understand it,
    >
    > 'ref' parameter passes the object into the Method by Reference, ie passes
    > the memory location of the original object in the parameter list. The[/color]
    Method[color=blue]
    > may change the value of the object and by doing so, will be changing the
    > value of the original object.
    >
    > 'out' parameter passes an object BY VALUE back into the object in the
    > calling parameters.
    >
    > 1) Am I right?
    > 2) Is it true to say that you are usually better to use 'out' unless the
    > original value of the parameter being passed in effects its value when you
    > pass it back. Or if your method needs to return more than one calculated
    > result.
    >
    > eg
    >
    > private void GetPosition(str ing lorryId, out int xLocation, out int
    > yLocation)
    >
    > Hope this makes sense, I'm very tired...
    >
    > Thanks,
    >
    > Chris.
    >
    >[/color]


    Comment

    • Jorge L Matos

      #3
      RE: Difference between 'out' and 'ref' parameters.

      You're almost right, "ref" and "out" are similar in that they can both return
      a value from a method, but they have slightly different semantics.

      You need to initialize a "ref" parameter before passing it to a method, but
      you don't have to initialize an "out" parameter before passing it to a method
      because the c# compiler knows that the method will assign a value to the
      parameter before returning. In fact, the c# compiler will throw a compiler
      error if it detects that the method has not assigned a value to an "out"
      parameter before returning.

      By value example: The output from the code below will be "x = 0"

      int x = 0;
      add(x)
      Console.Write(" x = {0}", x);

      void add(ref x)
      {
      x = x + 1
      }

      "Ref" example: The output from the code below will be "x = 1"

      int x = 0;
      add(ref x)
      Console.Write(" x = {0}", x);

      void add(ref x)
      {
      x = x + 1
      }

      "out" example: The output from the code below will be "x = 1"

      int x;
      add(out x)
      Console.Write(" x = {0}", x);

      void add(out x)
      {
      x = x + 1
      }

      Note: The variable "x" is not initialized prior to the call to "add"

      For more info:
      http://msdn.microsoft.com/library/de...l/vclrfref.asp

      "Chris Mayers" wrote:
      [color=blue]
      > 'out' and 'ref' parameters in C#...
      >
      > Both these can be used to pass parameter values BACK from a Method, but
      > obviously they are different techniques.
      >
      > As I understand it,
      >
      > 'ref' parameter passes the object into the Method by Reference, ie passes
      > the memory location of the original object in the parameter list. The Method
      > may change the value of the object and by doing so, will be changing the
      > value of the original object.
      >
      > 'out' parameter passes an object BY VALUE back into the object in the
      > calling parameters.
      >
      > 1) Am I right?
      > 2) Is it true to say that you are usually better to use 'out' unless the
      > original value of the parameter being passed in effects its value when you
      > pass it back. Or if your method needs to return more than one calculated
      > result.
      >
      > eg
      >
      > private void GetPosition(str ing lorryId, out int xLocation, out int
      > yLocation)
      >
      > Hope this makes sense, I'm very tired...
      >
      > Thanks,
      >
      > Chris.
      >
      >
      >[/color]

      Comment

      • Chris Mayers

        #4
        Re: Difference between 'out' and 'ref' parameters.

        Thanks for your answer, that has helped a lot.

        There does seem to be another difference though,

        this is legal (Compiler allows it)

        private int DoStuff(ref string x)
        {
        x = x + ":";
        return -1;
        }

        wheras this will not compile:

        private int DoStuff(out string x)
        {
        x = x + ":";
        return -1;
        }

        (says " 'Use of unassigned local vairable 'x' ")

        This, however is fine:

        private into DoStuff(out string x)
        {
        x = "1";
        return -1;
        }

        So you can't pass a value INTO a method using a 'out' parameter like you can
        with a 'ref' parameter. (?) I guess that's why its called 'out' :-)

        Chris


        Comment

        • Sean Hederman

          #5
          Re: Difference between 'out' and 'ref' parameters.

          "Chris Mayers" <chris_mayersBL UE@SUEDEYahoo.C om> wrote in message
          news:%23f55dwBR FHA.3708@TK2MSF TNGP15.phx.gbl. ..
          [Snip][color=blue]
          > So you can't pass a value INTO a method using a 'out' parameter like you
          > can
          > with a 'ref' parameter. (?) I guess that's why its called 'out' :-)[/color]

          Exactly. An out parameter for your function acts like an uninitialized
          variable. You have to initialize it before using it. The corollary is that
          the caller does not have to initialize the value they pass in. With ref, the
          caller must initialize the variable, and the callee doesn't have to.
          [color=blue]
          > Chris
          >
          >[/color]


          Comment

          • Jorge L Matos

            #6
            Re: Difference between 'out' and 'ref' parameters.

            I'm glad I helped you out, and it sounds like you've got a good understanding
            of the differences between "ref" and "out" now.

            "Chris Mayers" wrote:
            [color=blue]
            > Thanks for your answer, that has helped a lot.
            >
            > There does seem to be another difference though,
            >
            > this is legal (Compiler allows it)
            >
            > private int DoStuff(ref string x)
            > {
            > x = x + ":";
            > return -1;
            > }
            >
            > wheras this will not compile:
            >
            > private int DoStuff(out string x)
            > {
            > x = x + ":";
            > return -1;
            > }
            >
            > (says " 'Use of unassigned local vairable 'x' ")
            >
            > This, however is fine:
            >
            > private into DoStuff(out string x)
            > {
            > x = "1";
            > return -1;
            > }
            >
            > So you can't pass a value INTO a method using a 'out' parameter like you can
            > with a 'ref' parameter. (?) I guess that's why its called 'out' :-)
            >
            > Chris
            >
            >
            >[/color]

            Comment

            • Chris Mayers

              #7
              Jorge: Difference between 'out' and 'ref' parameters.

              Jorge,

              Did you post a reply to this?
              I saw somthing from you, but the news-server seemed to delete it before my
              news reader got a chance to down load it...??


              "Chris Mayers" <chris_mayersBL UE@SUEDEYahoo.C om> wrote in message
              news:%23f55dwBR FHA.3708@TK2MSF TNGP15.phx.gbl. ..[color=blue]
              > Thanks for your answer, that has helped a lot.
              >
              > There does seem to be another difference though,
              >
              > this is legal (Compiler allows it)
              >
              > private int DoStuff(ref string x)
              > {
              > x = x + ":";
              > return -1;
              > }
              >
              > wheras this will not compile:
              >
              > private int DoStuff(out string x)
              > {
              > x = x + ":";
              > return -1;
              > }
              >
              > (says " 'Use of unassigned local vairable 'x' ")
              >
              > This, however is fine:
              >
              > private into DoStuff(out string x)
              > {
              > x = "1";
              > return -1;
              > }
              >
              > So you can't pass a value INTO a method using a 'out' parameter like you[/color]
              can[color=blue]
              > with a 'ref' parameter. (?) I guess that's why its called 'out' :-)
              >
              > Chris
              >
              >[/color]


              Comment

              • Jorge L Matos

                #8
                RE: Jorge: Difference between 'out' and 'ref' parameters.

                Just a comment that I think you have a good understanding of the difference
                between "ref" and "out" now.

                "Chris Mayers" wrote:
                [color=blue]
                > Jorge,
                >
                > Did you post a reply to this?
                > I saw somthing from you, but the news-server seemed to delete it before my
                > news reader got a chance to down load it...??
                >
                >
                > "Chris Mayers" <chris_mayersBL UE@SUEDEYahoo.C om> wrote in message
                > news:%23f55dwBR FHA.3708@TK2MSF TNGP15.phx.gbl. ..[color=green]
                > > Thanks for your answer, that has helped a lot.
                > >
                > > There does seem to be another difference though,
                > >
                > > this is legal (Compiler allows it)
                > >
                > > private int DoStuff(ref string x)
                > > {
                > > x = x + ":";
                > > return -1;
                > > }
                > >
                > > wheras this will not compile:
                > >
                > > private int DoStuff(out string x)
                > > {
                > > x = x + ":";
                > > return -1;
                > > }
                > >
                > > (says " 'Use of unassigned local vairable 'x' ")
                > >
                > > This, however is fine:
                > >
                > > private into DoStuff(out string x)
                > > {
                > > x = "1";
                > > return -1;
                > > }
                > >
                > > So you can't pass a value INTO a method using a 'out' parameter like you[/color]
                > can[color=green]
                > > with a 'ref' parameter. (?) I guess that's why its called 'out' :-)
                > >
                > > Chris
                > >
                > >[/color]
                >
                >
                >[/color]

                Comment

                • Chris Mayers

                  #9
                  Re: Difference between 'out' and 'ref' parameters.

                  Thanks everyone for your help.

                  Jorge:Your message dissapeared again from my newsreader (Outlook Express),
                  but I managed to find in on Google Groups...

                  I shall go and find somthing else to try and understand now...

                  Cheers,

                  Chris.

                  "Chris Mayers" <chris_mayersBL UE@SUEDEYahoo.C om> wrote in message
                  news:uXEbeOCRFH A.1564@TK2MSFTN GP09.phx.gbl...[color=blue]
                  > Jorge,
                  >
                  > Did you post a reply to this?
                  > I saw somthing from you, but the news-server seemed to delete it before my
                  > news reader got a chance to down load it...??
                  >
                  >
                  > "Chris Mayers" <chris_mayersBL UE@SUEDEYahoo.C om> wrote in message
                  > news:%23f55dwBR FHA.3708@TK2MSF TNGP15.phx.gbl. ..[color=green]
                  > > Thanks for your answer, that has helped a lot.
                  > >
                  > > There does seem to be another difference though,
                  > >
                  > > this is legal (Compiler allows it)
                  > >
                  > > private int DoStuff(ref string x)
                  > > {
                  > > x = x + ":";
                  > > return -1;
                  > > }
                  > >
                  > > wheras this will not compile:
                  > >
                  > > private int DoStuff(out string x)
                  > > {
                  > > x = x + ":";
                  > > return -1;
                  > > }
                  > >
                  > > (says " 'Use of unassigned local vairable 'x' ")
                  > >
                  > > This, however is fine:
                  > >
                  > > private into DoStuff(out string x)
                  > > {
                  > > x = "1";
                  > > return -1;
                  > > }
                  > >
                  > > So you can't pass a value INTO a method using a 'out' parameter like you[/color]
                  > can[color=green]
                  > > with a 'ref' parameter. (?) I guess that's why its called 'out' :-)
                  > >
                  > > Chris
                  > >
                  > >[/color]
                  >
                  >[/color]


                  Comment

                  Working...