61

Assuming I have an Object ItemVO in which there a bunch of properties already assigned. eg:

ItemVO originalItemVO = new ItemVO();
originalItemVO.ItemId = 1;
originalItemVO.ItemCategory = "ORIGINAL";

I would like to create another duplicate by using :

duplicateItemVO = originalItemVO;

and then use the duplicateItemVO and alter its' properties, WITHOUT changing the originalItemVO:

// This also change the originalItemVO.ItemCategory which I do not want.
duplicateItemVO.ItemCategory = "DUPLICATE" 

How can I achieve this, without changing the class ItemVO ?

Thanks

public class ItemVO     
{
    public ItemVO()
    {
        ItemId = "";
        ItemCategory = "";
    }

    public string ItemId { get; set; }
    public string ItemCategory { get; set; }
}
Austin Salonen
  • 49,173
  • 15
  • 109
  • 139
Gotcha
  • 1,039
  • 1
  • 16
  • 24
  • 5
    Possible duplicate: http://stackoverflow.com/questions/78536/cloning-objects-in-c-sharp – Kekoa Feb 10 '12 at 18:47

8 Answers8

35

In order to change one instance without changing the other you need to clone the actual values of this instance and not the reference. The pattern used in .Net is to implement ICloneable. So your code would look like this:

public class ItemVO: ICloneable
  {
    public ItemVO()
    {
        ItemId = ""; 
        ItemCategory = ""; 
    }

    public string ItemId { get; set; }
    public string ItemCategory { get; set; }

    public object Clone()
    {
        return new ItemVO
        {
            ItemId = this.ItemId,
            ItemCategory = this.ItemCategory
        }; 
    }
 }

Now notice that you need an explicit cast when using Clone() (or you can make your own that returns ItemVO).

duplicateItemVO = (ItemVO) originalItemVO.Clone(); 
Sofian Hnaide
  • 2,284
  • 16
  • 13
  • This makes sense but assuming I cant change the class ItemVO , how else can I achieve this? – Gotcha Feb 10 '12 at 20:38
  • 1
    In this case you have one of two options: (1) extend the class and then implement the IColneable from the child class. (2) you have to recreate the object manually every time you need to assign it, to create the object manually you just use the same code in the Clone method. I presonally prefer the first option, specially if you end up doing this in many places. – Sofian Hnaide Feb 10 '12 at 21:16
  • Maybe evenduplicateItemVO = originalItemVO.Clone() as ItemVO; – mouldycurryness Dec 17 '19 at 14:50
28

To duplicate an object by value instead of reference, you can serialize it (e.g. JSON) and then deserialize it right after. You then have a copy by value.

Here's an example

ItemVO originalItemVO = new ItemVO();
originalItemVO.ItemId = 1;
originalItemVO.ItemCategory = "ORIGINAL";

string json = Newtonsoft.Json.JsonConvert.SerializeObject(originalItemVO); 
ItemVO duplicateItemVO = Newtonsoft.Json.JsonConvert.DeserializeObject<ItemVO>(json);
Mason
  • 1,007
  • 1
  • 13
  • 31
Amit
  • 289
  • 3
  • 2
  • 3
    Welcome to the SO! Please consider adding some explanation or commentary to your answer. Generally it is discourage to post an answer with just a raw code and nothing else. Additionally, if you answer a dated question with plenty of answer consider if you bring anything new to the topic (there is a very similar answer that uses de-serialization). – Aki Jan 03 '20 at 13:50
  • This has to be one of the slowest ways to do this. It looks clean, yet this goes over reflecting the objects/types in question and then performing costly serialization to text and back to memory. Reflection could at least do this directly on memory, without relying on text.. – Vinz Oct 07 '22 at 16:45
27

You would need to construct a new instance of your class, not just assign the variable:

duplicateItemVO = new ItemVO 
    { 
        ItemId = originalItemVO.ItemId, 
        ItemCategory = originalItemVO.ItemCategory 
    };

When you're dealing with reference types (any class), just assigning a variable is creating a copy of the reference to the original object. As such, setting property values within that object will change the original as well. In order to prevent this, you need to actually construct a new object instance.

Reed Copsey
  • 554,122
  • 78
  • 1,158
  • 1,373
  • 1
    Is there another way rather than explicitly assigning each property in originalItemVO to duplicateItemVO? I've used an itemVO with 2 properties for simplicity... but I have about 30+ – Gotcha Feb 10 '12 at 20:34
  • 3
    @Gotcha Create a constructor in your class that works off a second instance, and internally does the setting appropriately. That's the most maintainable method - then you'd just write: `duplicateItemVO = new ItemVO(originalItemVO);` – Reed Copsey Feb 10 '12 at 20:36
17

You cannot practically copy an object since they would more likely be of reference types. The ideal method is to serialize or stream the object into a new one - Provided your class is serializable (by providing the [Serializable] attribute in class declaration).

private static T Clone<T>(T source)
    {
        if (!typeof(T).IsSerializable)
        {
            throw new ArgumentException("The type must be serializable.", "source");
        }

        if (Object.ReferenceEquals(source, null))
        {
            return default(T);
        }

        System.Runtime.Serialization.IFormatter formatter = new System.Runtime.Serialization.Formatters.Binary.BinaryFormatter();
        Stream stream = new MemoryStream();
        using (stream)
        {
            formatter.Serialize(stream, source);
            stream.Seek(0, SeekOrigin.Begin);
            return (T)formatter.Deserialize(stream);
        }
    }

Now you can use this code:

[Serializable]
public class MyClass
{
  public int a {get; set;}
  public int b {get; set;}
}     

var obj = new MyClass{
a = 10,
b = 20,
};

var newobj = Clone<MyClass>(obj);

You will get an entirely new copy of obj. Note: Any other class inside MyClass must also be declared with the attribute [Serializable].

Venugopal M
  • 2,280
  • 1
  • 20
  • 30
13

I suggest using as is in the link below. For me it worked very well.

https://msdn.microsoft.com/en-us/library/system.object.memberwiseclone(v=vs.110).aspx

 public Person ShallowCopy ()
 {
    return (Person) this.MemberwiseClone ();
 }
Luiz Baião
  • 131
  • 1
  • 4
5

class are reference type and when you change one instance it will change the ariginal refference. so use value type object for overcome your task(ex: use struct instead of class)

public struct ItemVO { *** }

or you can implement ICloneable Interface for your class

Chamika Sandamal
  • 23,565
  • 5
  • 63
  • 86
  • Right, i've figured this much... I'd rather not change this class to a struct to avoid other impact of this change. I'm only working on a small portion of the code which has this existing class...which I cannot modify. How else would i achieve this? – Gotcha Feb 10 '12 at 20:37
  • you can implement [ICloneable Interface](http://msdn.microsoft.com/en-us/library/system.icloneable.aspx) for your class – Chamika Sandamal Feb 11 '12 at 04:34
2

By Default objects are reference type.

Assign the one object to another object its means that you just refer the address of the object.Any changes in any object it will reflect in both.

To solve this problem you should have initialize the object using "new" keyword, then add this object value in the first object.

2

As of c# 4.5 the base class object contains a method called MemberwiseClone that enables you to perform a shallow copy of that object and returns the result as a new instance. (If a field is a value type, a bit-by-bit copy of the field is performed. If a field is a reference type, the reference is copied but the referred object is not; therefore, the original object and its clone refer to the same object.)

This is useful if you are looking to implement a prototype design pattern.

If you are looking to implement a deep copy (everything within the class is duplicated as new instances) then serialization or reflection are probably the best tools

Jamie Pearcey
  • 345
  • 1
  • 10