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. Discussion: What is the default type (ref or value) for parameters passed to function??

Discussion: What is the default type (ref or value) for parameters passed to function??

Scheduled Pinned Locked Moved C#
questiondiscussion
8 Posts 4 Posters 0 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.
  • C Offline
    C Offline
    Cracked Down
    wrote on last edited by
    #1

    Hi all, Let’s discuss and share your knowledge on this question!! I am sure most of the member having this question in their mind and as many as members knows the answer for this!! Mean while, you can also ask your queries to other on same topic!!!

    D S E 3 Replies Last reply
    0
    • C Cracked Down

      Hi all, Let’s discuss and share your knowledge on this question!! I am sure most of the member having this question in their mind and as many as members knows the answer for this!! Mean while, you can also ask your queries to other on same topic!!!

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

      By default, parameters are passed by value. Click me[^]

      A guide to posting questions on CodeProject[^]
      Dave Kreskowiak Microsoft MVP Visual Developer - Visual Basic
           2006, 2007, 2008

      S 1 Reply Last reply
      0
      • C Cracked Down

        Hi all, Let’s discuss and share your knowledge on this question!! I am sure most of the member having this question in their mind and as many as members knows the answer for this!! Mean while, you can also ask your queries to other on same topic!!!

        S Offline
        S Offline
        S Senthil Kumar
        wrote on last edited by
        #3

        Depends on whether the parameter is a reference type or a value type?

        Regards Senthil [MVP - Visual C#] _____________________________ My Home Page |My Blog | My Articles | My Flickr | WinMacro

        1 Reply Last reply
        0
        • C Cracked Down

          Hi all, Let’s discuss and share your knowledge on this question!! I am sure most of the member having this question in their mind and as many as members knows the answer for this!! Mean while, you can also ask your queries to other on same topic!!!

          E Offline
          E Offline
          Eslam Afifi
          wrote on last edited by
          #4

          By default, by value (a copy). If the parameter is a value type, it's a "copy" of that value. If the parameter is a reference type, it's a "copy" of that reference (by reference). If you use ref or out: If the parameter is a value type, you pass a reference (address) to it. If the parameter is a reference type, you pass a reference to this original reference which refers to the object. As an example of this, write a function that takes 2 arrays and swaps them (without copying the elements).

          Eslam Afifi

          C 1 Reply Last reply
          0
          • D Dave Kreskowiak

            By default, parameters are passed by value. Click me[^]

            A guide to posting questions on CodeProject[^]
            Dave Kreskowiak Microsoft MVP Visual Developer - Visual Basic
                 2006, 2007, 2008

            S Offline
            S Offline
            S Senthil Kumar
            wrote on last edited by
            #5

            Groan.. I know where you're coming from - for reference types, references to objects are passed by value by default. "Passed by reference" is not entirely accurate, but it describes the object sharing aspect well.

            Regards Senthil [MVP - Visual C#] _____________________________ My Home Page |My Blog | My Articles | My Flickr | WinMacro

            1 Reply Last reply
            0
            • E Eslam Afifi

              By default, by value (a copy). If the parameter is a value type, it's a "copy" of that value. If the parameter is a reference type, it's a "copy" of that reference (by reference). If you use ref or out: If the parameter is a value type, you pass a reference (address) to it. If the parameter is a reference type, you pass a reference to this original reference which refers to the object. As an example of this, write a function that takes 2 arrays and swaps them (without copying the elements).

              Eslam Afifi

              C Offline
              C Offline
              Cracked Down
              wrote on last edited by
              #6

              If reference(from stack) to the reference type(e.g object)is passed then, whatever changes done inside the functions are reflected to the object (as another copy of reference is created on stack which also point to original location e.g object)..........isn't it?? then what is the use of ref and out keyword here??? anybody like share??

              E S 2 Replies Last reply
              0
              • C Cracked Down

                If reference(from stack) to the reference type(e.g object)is passed then, whatever changes done inside the functions are reflected to the object (as another copy of reference is created on stack which also point to original location e.g object)..........isn't it?? then what is the use of ref and out keyword here??? anybody like share??

                E Offline
                E Offline
                Eslam Afifi
                wrote on last edited by
                #7

                class A
                {
                public Guid Guid;
                public A()
                {
                Guid = Guid.NewGuid();
                }
                }

                static void Main(string[] args)
                {
                var a = new A();
                Console.WriteLine(a.Guid);
                Foo(a);
                Console.WriteLine(a.Guid);
                }

                static void Foo(A par)
                {
                Console.WriteLine(par.Guid);
                par = new A();
                Console.WriteLine(par.Guid);
                }

                static void Main(string[] args)
                {
                var a = new A();
                Console.WriteLine(a.Guid);
                Foo(ref a);
                Console.WriteLine(a.Guid);
                }

                static void Foo(ref A par)
                {
                Console.WriteLine(par.Guid);
                par = new A();
                // btw, you still can do something like par.Guid = Guid.NewGuid();
                Console.WriteLine(par.Guid);
                }

                Eslam Afifi

                modified on Thursday, March 19, 2009 10:48 AM

                1 Reply Last reply
                0
                • C Cracked Down

                  If reference(from stack) to the reference type(e.g object)is passed then, whatever changes done inside the functions are reflected to the object (as another copy of reference is created on stack which also point to original location e.g object)..........isn't it?? then what is the use of ref and out keyword here??? anybody like share??

                  S Offline
                  S Offline
                  S Senthil Kumar
                  wrote on last edited by
                  #8

                  deep@Pune wrote:

                  then what is the use of ref and out keyword here???

                  ref and out pass the reference to the reference by value :)

                  Regards Senthil [MVP - Visual C#] _____________________________ My Home Page |My Blog | My Articles | My Flickr | WinMacro

                  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