seanperry

Hi All,

I've looked through the online documentation and read through a lot of explanation but still its not making any sense to me about what they mean. Probably just documentation no example.
If someone could show me with the proper examples as how they differ.

I would like to see the results with examples how they differ from each other.
I'd be ever so appreciative!

Many thanks.


Re: Visual C# General Deep copy vs Shallow copy

boban.s

If you have an object (instance of some class) with values and you want to create a copy of that object in another variable from same type, then if you Shallow copy, all property values which are of value types will be copied, but if you have a property which is of reference type (instance of another class) then this instance will not be copied, instead you will have a reference to that instance.
You probably now know what is Deep Clone.






Re: Visual C# General Deep copy vs Shallow copy

seanperry

Can you give some examples please as I would like to see what happens

As you mentioned (if you have a property which is of reference type then this instance will not be copied but what I read so far says reference will also be copied)

I am trying to write few things where I could see the difference but being new to this language nothing is working at all.

Example would be helpful if you give an example what you mentioned about above scenario.

Many thanks indeed.





Re: Visual C# General Deep copy vs Shallow copy

boban.s

Here is good explanation of what is the difference between both type of copying objects:
http://codeidol.com/csharp/csharpckbk2/Classes-and-Structures/Building-Cloneable-Classes/

Here is simple console app for test:

using System;

using System.Collections.Generic;

using System.Globalization;

using System.IO;

using System.Runtime.Serialization.Formatters.Binary;


-----------------------

private static void Main()

{

A a = new A(1, true, 1m, 5);

A ashallow = a.ShallowCopy();

A adeep = a.DeepClone();

a.ReferenceProperty.DecimalProperty = 2m;

Console.WriteLine(ashallow.ReferenceProperty.DecimalProperty);

//this will print 2 because ashallow have a pointer to referenceproperty of a

Console.WriteLine(adeep.ReferenceProperty.DecimalProperty);

//this will print 1 because adeep have a new instance created with values of referenceproperty of a

Console.ReadLine();

}


//Definition of classes:

[Serializable]

public class A : object

{

private int _IntProperty;

public int IntProperty

{

get { return _IntProperty; }

set { _IntProperty = value; }

}

private bool _BoolProperty;

public bool BoolProperty

{

get { return _BoolProperty; }

set { _BoolProperty = value; }

}

private B _ReferenceProperty;

public B ReferenceProperty

{

get { return _ReferenceProperty; }

set { _ReferenceProperty = value; }

}

public A(int intvalue, bool boolvalue, decimal decimalvalue, short shortvalue)

{

_IntProperty = intvalue;

_BoolProperty = boolvalue;

_ReferenceProperty = new B(decimalvalue, shortvalue);

}

public A DeepClone()

{

MemoryStream m = new MemoryStream();

BinaryFormatter b = new BinaryFormatter();

b.Serialize(m, this);

m.Position = 0;

return (A)b.Deserialize(m);

}

public A ShallowCopy()

{

return (A)MemberwiseClone();

}

}

[Serializable]

public class B : object

{

private decimal _DecimalProperty;

public decimal DecimalProperty

{

get { return _DecimalProperty; }

set { _DecimalProperty = value; }

}

private short _ShortProperty;

public B(decimal decimalvalue, short shortvalue)

{

_DecimalProperty = decimalvalue;

_ShortProperty = shortvalue;

}

public short ShortProperty

{

get { return _ShortProperty; }

set { _ShortProperty = value; }

}

}






Re: Visual C# General Deep copy vs Shallow copy

seanperry

Thanks Boban,

Really appreciate that. It would have been quite easier for me had you been given me a simple example.

Every one would really appeciate if things are simple and really straight forward.

Please specify if possible in a simple manner so that me and peolpe like me understand and use this as reference.

It's for anyone who could specify in simple manner and I can say " oh yeah I got it"