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
CODE PROJECT For Those Who Code
  • Home
  • Articles
  • FAQ
Community
  1. Home
  2. General Programming
  3. C#
  4. REF AND OUT VARIABLES

REF AND OUT VARIABLES

Scheduled Pinned Locked Moved C#
question
11 Posts 5 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.
  • R Offline
    R Offline
    RINSON VARGHESE
    wrote on last edited by
    #1

    what is the difference between ref and out variables(in situation basis)....i know that ref should be initialized before its usage ...why its like that(what is the need of initializing ref)?

    L M G 3 Replies Last reply
    0
    • R RINSON VARGHESE

      what is the difference between ref and out variables(in situation basis)....i know that ref should be initialized before its usage ...why its like that(what is the need of initializing ref)?

      L Offline
      L Offline
      Luc Pattyn
      wrote on last edited by
      #2

      VARGHESERINSON wrote:

      why its like that(what is the need of initializing ref)?

      ref = in + out in ==> must have value the C# documentation explains all. :)

      Luc Pattyn


      I only read code that is properly indented, and rendered in a non-proportional font; hint: use PRE tags in forum messages


      Local announcement (Antwerp region): Lange Wapper? 59.24% waren verstandig genoeg om NEEN te stemmen; bye bye viaduct.


      1 Reply Last reply
      0
      • R RINSON VARGHESE

        what is the difference between ref and out variables(in situation basis)....i know that ref should be initialized before its usage ...why its like that(what is the need of initializing ref)?

        M Offline
        M Offline
        musefan
        wrote on last edited by
        #3

        Well an OUT wont compile if you don't set it in the function, a REF does not need settings. Thou, why would you use a REF if you didn't plan on using it anyway. Maybe one path uses it, other path does not. Maybe a better example would be if you were to re-use the value in other functions, for example consider the following sample...

        ref int SomeData;

        public void Setup(ref int data)
        {
        SomeData = data;
        }

        public void Add5()
        {
        SomeData += 5;
        }

        ...pointless? Yes. Doe's it compile? I don't know. But would not work with the OUT parameter. [EDIT] It does not compile Next attempt... If you where to design a class library that was to be used for somebody else then it could be useful. You could use REF to ensure that the value was set before passing in, and you could use out if you didn't care what the value was to start with. Examples...

        public void (out int i)
        {
        i = 10;
        }

        public void (ref int i)
        {
        i = i + 5;//would break if not already set
        }

        ...Maybe this is what Luc was getting at [EDIT] Just me again... I worked out a better usage. Consider the following struct instead of a basic datatype.

        struct Sample{
        public string A;
        public string B;

        public Sample(string a, string b)
        {
        A = a;
        B = b;
        }
        }

        If you use this struct with REF, then you can change B and keep A the same, if you use OUT then you are required to initialize the data and thus lose all data. This is the same for passing Controls (for whatever reason you would want to)

        Life goes very fast. Tomorrow, today is already yesterday.

        modified on Wednesday, October 21, 2009 9:12 AM

        L 1 Reply Last reply
        0
        • R RINSON VARGHESE

          what is the difference between ref and out variables(in situation basis)....i know that ref should be initialized before its usage ...why its like that(what is the need of initializing ref)?

          G Offline
          G Offline
          Ghydo
          wrote on last edited by
          #4

          "out" parameters are like return values, you must assign them before returning. "ref" causes arguments to be passed by reference: any changes made to the parameter in the method will be reflected in that variable.

          G 1 Reply Last reply
          0
          • G Ghydo

            "out" parameters are like return values, you must assign them before returning. "ref" causes arguments to be passed by reference: any changes made to the parameter in the method will be reflected in that variable.

            G Offline
            G Offline
            Ghydo
            wrote on last edited by
            #5

            "out" is used when you need to return more values to the caller (for example a return code and other data), "ref" is used when the method can change the argument.

            M 1 Reply Last reply
            0
            • G Ghydo

              "out" is used when you need to return more values to the caller (for example a return code and other data), "ref" is used when the method can change the argument.

              M Offline
              M Offline
              musefan
              wrote on last edited by
              #6

              hmmm... out will also produce the same result, i.e. changing the argument

              Life goes very fast. Tomorrow, today is already yesterday.

              G 1 Reply Last reply
              0
              • M musefan

                hmmm... out will also produce the same result, i.e. changing the argument

                Life goes very fast. Tomorrow, today is already yesterday.

                G Offline
                G Offline
                Ghydo
                wrote on last edited by
                #7

                "out" arguments are one-way, "ref" are bidirectional.

                M L 2 Replies Last reply
                0
                • G Ghydo

                  "out" arguments are one-way, "ref" are bidirectional.

                  M Offline
                  M Offline
                  musefan
                  wrote on last edited by
                  #8

                  I did think of bringing that up, but when would you be able to alter the value from outside the function while the function is running? You would need multi-thread, in which case there would be other issues.

                  Life goes very fast. Tomorrow, today is already yesterday.

                  S 1 Reply Last reply
                  0
                  • G Ghydo

                    "out" arguments are one-way, "ref" are bidirectional.

                    L Offline
                    L Offline
                    Luc Pattyn
                    wrote on last edited by
                    #9

                    as in "ref = in + out" ? :)

                    Luc Pattyn


                    I only read code that is properly indented, and rendered in a non-proportional font; hint: use PRE tags in forum messages


                    Local announcement (Antwerp region): Lange Wapper? 59.24% waren verstandig genoeg om NEEN te stemmen; bye bye viaduct.


                    1 Reply Last reply
                    0
                    • M musefan

                      Well an OUT wont compile if you don't set it in the function, a REF does not need settings. Thou, why would you use a REF if you didn't plan on using it anyway. Maybe one path uses it, other path does not. Maybe a better example would be if you were to re-use the value in other functions, for example consider the following sample...

                      ref int SomeData;

                      public void Setup(ref int data)
                      {
                      SomeData = data;
                      }

                      public void Add5()
                      {
                      SomeData += 5;
                      }

                      ...pointless? Yes. Doe's it compile? I don't know. But would not work with the OUT parameter. [EDIT] It does not compile Next attempt... If you where to design a class library that was to be used for somebody else then it could be useful. You could use REF to ensure that the value was set before passing in, and you could use out if you didn't care what the value was to start with. Examples...

                      public void (out int i)
                      {
                      i = 10;
                      }

                      public void (ref int i)
                      {
                      i = i + 5;//would break if not already set
                      }

                      ...Maybe this is what Luc was getting at [EDIT] Just me again... I worked out a better usage. Consider the following struct instead of a basic datatype.

                      struct Sample{
                      public string A;
                      public string B;

                      public Sample(string a, string b)
                      {
                      A = a;
                      B = b;
                      }
                      }

                      If you use this struct with REF, then you can change B and keep A the same, if you use OUT then you are required to initialize the data and thus lose all data. This is the same for passing Controls (for whatever reason you would want to)

                      Life goes very fast. Tomorrow, today is already yesterday.

                      modified on Wednesday, October 21, 2009 9:12 AM

                      L Offline
                      L Offline
                      Luc Pattyn
                      wrote on last edited by
                      #10

                      musefan wrote:

                      Maybe this is what Luc was getting at

                      What Luc was getting at most of all is: read the fine manual, don't be so damned lazy. And read a book about the programming language you want to use; this is after all basic knowledge. :~

                      Luc Pattyn


                      I only read code that is properly indented, and rendered in a non-proportional font; hint: use PRE tags in forum messages


                      Local announcement (Antwerp region): Lange Wapper? 59.24% waren verstandig genoeg om NEEN te stemmen; bye bye viaduct.


                      1 Reply Last reply
                      0
                      • M musefan

                        I did think of bringing that up, but when would you be able to alter the value from outside the function while the function is running? You would need multi-thread, in which case there would be other issues.

                        Life goes very fast. Tomorrow, today is already yesterday.

                        S Offline
                        S Offline
                        Saksida Bojan
                        wrote on last edited by
                        #11

                        Int32 someInt = 6;
                        SomeMethod(ref someInt);
                        MessageBox.Show("SomeInt now contains value" + someInt.ToString()); // It has value of 12
                        // And now for method
                        public void SomeMethod(ref Int32 data)
                        {
                        data = data * 2;
                        }

                        musefan wrote:

                        I did think of bringing that up, but when would you be able to alter the value from outside the function while the function is running? You would need multi-thread, in which case there would be other issues.

                        that is the same think as using static, I really don't remember if it compiles if you access directly from different thread, but it is a programmer job to make it Thread Safe. Also as I think accessing a method in different thread you need to use Invoke() method, at least it is one of three possible ways

                        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