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 by value

Pass by value

Scheduled Pinned Locked Moved C#
csharpasp-netperformancehelptutorial
4 Posts 3 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.
  • J Offline
    J Offline
    jon 80
    wrote on last edited by
    #1

    "One notable quirk occurs when you use the standard pass-by-value mechanism. In this e, pass-by-value doesn’t create a copy of the object, but a copy of the reference. This refer- e still points to the same in-memory object. This means that if you pass a Product object t ethod, for example, the method will be able to alter your Product object, regardless of ther you use pass-by-value or pass-by-reference." Beginning ASP.NET 3.5 in C# 2008 (ISBN-13 (pbk): 978-1-59059-891-7) P.69 Can anyone provide a code snippet to demonstrate the above? If the above were a valid statement, wouldn't this be a bug in the .NET 3.5 framework?

    Jon

    J D 2 Replies Last reply
    0
    • J jon 80

      "One notable quirk occurs when you use the standard pass-by-value mechanism. In this e, pass-by-value doesn’t create a copy of the object, but a copy of the reference. This refer- e still points to the same in-memory object. This means that if you pass a Product object t ethod, for example, the method will be able to alter your Product object, regardless of ther you use pass-by-value or pass-by-reference." Beginning ASP.NET 3.5 in C# 2008 (ISBN-13 (pbk): 978-1-59059-891-7) P.69 Can anyone provide a code snippet to demonstrate the above? If the above were a valid statement, wouldn't this be a bug in the .NET 3.5 framework?

      Jon

      J Offline
      J Offline
      jgauffin
      wrote on last edited by
      #2

      It's true for strings and primitives. What are they passing?

      J 1 Reply Last reply
      0
      • J jgauffin

        It's true for strings and primitives. What are they passing?

        J Offline
        J Offline
        jon 80
        wrote on last edited by
        #3

        I haven't tried it as yet, that's why I asked for a code sample, because I want to understand.

        Jon

        1 Reply Last reply
        0
        • J jon 80

          "One notable quirk occurs when you use the standard pass-by-value mechanism. In this e, pass-by-value doesn’t create a copy of the object, but a copy of the reference. This refer- e still points to the same in-memory object. This means that if you pass a Product object t ethod, for example, the method will be able to alter your Product object, regardless of ther you use pass-by-value or pass-by-reference." Beginning ASP.NET 3.5 in C# 2008 (ISBN-13 (pbk): 978-1-59059-891-7) P.69 Can anyone provide a code snippet to demonstrate the above? If the above were a valid statement, wouldn't this be a bug in the .NET 3.5 framework?

          Jon

          D Offline
          D Offline
          Deepak VS
          wrote on last edited by
          #4

          Pass by value and pass by reference: Passing mechanisms decide how changes made to the parameter affect the caller. Consider the following code snippet Ex: class MyClass { public int value; } class Program { ..void funcPassbyValue(MyClass obj) { obj.value = 10; obj = new MyClass(); obj.value = 5; } ..void func2PassbyRef(ref MyClass obj) { obj.value = 10; obj = new MyClass(); obj.value = 5; } .. Main() { MyClass passedObj = new MyClass(); funcPassbyValue(passedObj ); Console.WriteLine(passedObj .value); //Outputs 10 func2PassbyRef(ref passedObj ); Console.WriteLine(passedObj .value); //Outputs 5 } } You can see both the methods funcPassbyValue && func2PassbyRef differ only in their signature. Things which happen in common to both the methods: Any changes made to the argument to the method will be reflected back (condition is the changes are done while the parameter - in this case obj - is not changed to refer other object). what does funcPassbyValue do differently: any changes made to the parameter (obj) will be local within the method and will not change (change here means to make it refer other object) the passedObj to refer other object. Hope this clears your doublt. what does func2PassbyRefdo differently: any changes made to the parameter (obj) will be reflected back to the caller in this case passedObj is changed to refer to the new object created inside the method.

          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