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. pass an object by value.

pass an object by value.

Scheduled Pinned Locked Moved C#
csharptutorialquestion
9 Posts 7 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.
  • P Offline
    P Offline
    prasadbuddhika
    wrote on last edited by
    #1

    any one knows how to pass an object by value instead of reference in C#?

    P L S 3 Replies Last reply
    0
    • P prasadbuddhika

      any one knows how to pass an object by value instead of reference in C#?

      P Offline
      P Offline
      PIEBALDconsult
      wrote on last edited by
      #2

      Step 1) Ensure it's a value. Step 2) Pass it.

      1 Reply Last reply
      0
      • P prasadbuddhika

        any one knows how to pass an object by value instead of reference in C#?

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

        Hi, that is not possible. Not sure why you think you want that. Maybe what you need is a clone, i.e. a second object that is an exact copy of the original, so changing it would not change the original. I very seldom need that though. I suggest you explain the situation. :)

        Luc Pattyn [Forum Guidelines] [Why QA sucks] [My Articles]


        Getting an article published on CodeProject should be easier and faster for Bronze and Silver authors.


        P 1 Reply Last reply
        0
        • L Luc Pattyn

          Hi, that is not possible. Not sure why you think you want that. Maybe what you need is a clone, i.e. a second object that is an exact copy of the original, so changing it would not change the original. I very seldom need that though. I suggest you explain the situation. :)

          Luc Pattyn [Forum Guidelines] [Why QA sucks] [My Articles]


          Getting an article published on CodeProject should be easier and faster for Bronze and Silver authors.


          P Offline
          P Offline
          prasadbuddhika
          wrote on last edited by
          #4

          thanx, i just wanted to know whether it is possible or not , cloning means before sending we create a copy of the object and send it to the method. hmmm.. thanx.

          P D 2 Replies Last reply
          0
          • P prasadbuddhika

            thanx, i just wanted to know whether it is possible or not , cloning means before sending we create a copy of the object and send it to the method. hmmm.. thanx.

            P Offline
            P Offline
            Praveen Raghuvanshi
            wrote on last edited by
            #5

            Refer http://www.c-sharpcorner.com/UploadFile/rmcochran/chsarp\_memory401152006094206AM/chsarp\_memory4.aspx for an excellent explanation on clone.

            Praveen Raghuvanshi Software Engineer, India.

            1 Reply Last reply
            0
            • P prasadbuddhika

              thanx, i just wanted to know whether it is possible or not , cloning means before sending we create a copy of the object and send it to the method. hmmm.. thanx.

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

              prasadbuddhika wrote:

              cloning means before sending we create a copy of the object and send it to the method.

              Which is also passed by reference...

              A guide to posting questions on CodeProject[^]
              Dave Kreskowiak Microsoft MVP Visual Developer - Visual Basic
                   2006, 2007, 2008
              But no longer in 2009...

              1 Reply Last reply
              0
              • P prasadbuddhika

                any one knows how to pass an object by value instead of reference in C#?

                S Offline
                S Offline
                SBGTrading
                wrote on last edited by
                #7

                All parameters are passed by value in C#...you have to use additional modifiers in order for them to be passed by reference (use either "ref" or "out" keywords). http://msdn.microsoft.com/en-us/library/0f66670z(VS.71).aspx[^]

                P D 2 Replies Last reply
                0
                • S SBGTrading

                  All parameters are passed by value in C#...you have to use additional modifiers in order for them to be passed by reference (use either "ref" or "out" keywords). http://msdn.microsoft.com/en-us/library/0f66670z(VS.71).aspx[^]

                  P Offline
                  P Offline
                  PIEBALDconsult
                  wrote on last edited by
                  #8

                  But most of those values are references.

                  1 Reply Last reply
                  0
                  • S SBGTrading

                    All parameters are passed by value in C#...you have to use additional modifiers in order for them to be passed by reference (use either "ref" or "out" keywords). http://msdn.microsoft.com/en-us/library/0f66670z(VS.71).aspx[^]

                    D Offline
                    D Offline
                    DaveyM69
                    wrote on last edited by
                    #9

                    Everything that is passed is a value in the truest sense. However that value is (most) often a reference to the object rather than the 'value' itself. The only way to do what the op wants is to create a new object (therefore a new reference) and copy the values the object holds. Where the object's values are also references, new copies of those also need to be created. This needs to be done recursively until all object references have been recreated. This is known as deep cloning and, as you can see, can be quite an involved and complex task which is why there is no built in Clone method in the framework. The closest we have is ICloneable which has (of course as it's an interface) to be implemented by the creator of the class.

                    Dave

                    If this helped, please vote & accept answer!

                    Binging is like googling, it just feels dirtier. (Pete O'Hanlon)
                    BTW, in software, hope and pray is not a viable strategy. (Luc Pattyn)

                    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