in c#, after I add a object to a collection, if another copy(deep copy) is always created?
Share
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
No, if it is a class, most objects are, only a reference to the same object is stored in the collection.
If it is a value type, like int, double and structs a copy is made (not a deep copy, if the struct has a reference to a class object that in turn will not be copied).
Edit:
To deep copy objects you first need to create a deep copy function.
Have a look at Create a Deep Copy in C# or How to make a deep copy in C# ?
Then you can run your deep copy method before adding items to your collection.
Note
It is not very often you really need a true deep copy. Often it is better to rethink the dataflow in your application.