0

I have the following setup in an aspx page:

Object Original = obj;
System.Threading.Thread thread = new System.Threading.Thread(() => saveOriginalDetails(Original));
thread.Start();  

The basic idea of this is that I have an object, and I want to save it exactly how it is before making any changes to it.

So I make a copy of the original object obj and store it as Original

I am starting a new thread because the saveOriginalDetails method is slowing the code down too much.

My question is, if I do this instead:

System.Threading.Thread thread = new System.Threading.Thread(() => saveOriginalDetails(obj));
thread.Start(); 

obj.name = "NewName";

Where I am now passing in the original object, and copy it inside the method that is running concurrently, like this:

    private void saveOriginalDetails(object applicant)
    {
        object OriginalApplicant = applicant;
.....
}

Will the object passed in to the method:

saveOriginalDetails(obj));

Have the updated name value eg a name of newName ?

Alex
  • 3,730
  • 9
  • 43
  • 94
  • 2
    You're not copying the *object*, you're only copying its *reference*, you still only have 1 object in play, but now you have 2 references to it. If you modify the object, you still only modify the one object. – Lasse V. Karlsen Apr 07 '16 at 10:00
  • 3
    @M.kazemAkhgary: No, objects aren't passed by reference. References are passed by value. It's important to differentiate between those. See http://pobox.com/~skeet/csharp/parameters.html – Jon Skeet Apr 07 '16 at 10:04
  • @JonSkeet my mistake. i see. Mr Karlsen said it correct. thank you. @Alex if you want to copy, one way is to create new DuoApplicant by typing `new DuoApplicant(...);` and copying all properties manually if possible or try this way http://stackoverflow.com/a/16696564/4767498 – M.kazem Akhgary Apr 07 '16 at 10:10
  • Moreover when you pass object as parameter this happens in a *separate* thread with no guarantee that this object is the same. Your object should be thread-safe in this scenario and it's also more logical to pass copy of object to method rather then create this copy inside of it. – Fabjan Apr 07 '16 at 10:30

1 Answers1

1

First, don't use Thread. Use the new Task classes instead (if possible - you didn't specify which .NET version you are using).

Secondly, you're only passing a parameter to the saveOriginalDetails.

Lastly, if your class is a model class (it sort of looks this way) and is serializable, you can relatively quickly create a perfect copy by serializing it and deserializing it (which has the benefit of working with any future changes you might make to your class). A faster-working solution (which, however, would require more actual programming work) would be to write your own code for cloning your class. That said, unless your class is really really large and complex, serializing it and deserializing it, while not the most optimal solution, should be fast enough.

Finally, unless you have an actual business need to store a copy of the DuoApplicant object, an in-memory copy, as described above, should suffice.

MBender
  • 5,395
  • 1
  • 42
  • 69