Re: Framework 3.5 Copy POCO?

Trey Nash <[email protected]>
Newsgroups gmane.comp.windows.devel.dotnet.advanced
Message-ID <00fb01c93e96$ea778e10$bf66aa30$@com>
Be very careful using ICloneable and MemberwiseClone()!  Here are the
reasons why, which I touch upon in my book as well. (Maybe a plug, I
suppose. :-) )

1.  ICloneable carries with it no information as to whether calling the
Clone() method will give you a shallow or deep copy.  This can be a problem
for, say, objects that contain an instance of your type as one of their
fields.  Say they want to implement ICloneable with a deep copy.  The
obvious implementation is to iterate down through the object graph calling
Clone() on the member fields, including your type instance's Clone() method.
However, if you implement a shallow copy in Clone(), then they will never be
able to get a true deep copy of their own object that contains yours.
Believe it or not, the Framework Guidelines suggest not using the ICloneable
interface at all and this is one of the main reasons.  Check out the
following post by Brad Abrams on the subject:
http://blogs.msdn.com/brada/archive/2003/04/09/49935.aspx

2.  Yes, MemberwiseClone() only supports a shallow copy.  However, keep in
mind that a constructor on the new instance will not be called!  This is
problematic if you rely on the constructor to implement some sort of
side-effect.  So, say in your debug build you have some sort of static
unsigned int that you use to keep track of how many instance of this object
have been created in the current app domain that you use for diagnostic
purposes.  That count would then be off if you were to create a new instance
via MemberwiseClone() and the count is only incremented in the constructors
of the type.

Hope this helps, and be careful out there. :-)

	-Trey

---------------------------------------
Trey Nash

Check out my book "Accelerated C# 2008"
http://www.amazon.com/Accelerated-C-2008-Trey-Nash/dp/1590598733



> -----Original Message-----
> From: Discussion of advanced .NET topics. [mailto:ADVANCED-
> [email protected]] On Behalf Of Mike Andrews
> Sent: Tuesday, November 04, 2008 6:34 AM
> To: [email protected]
> Subject: Re: [ADVANCED-DOTNET] Framework 3.5 Copy POCO?
> 
> Implement ICloneable and then return the MemberwiseClone() method.
> However this only does a shallow copy and not a deep copy so if you
> have
> properties that are reference types, you'll get the same reference
> again in
> the cloned copy.
> 
> On Tue, Nov 4, 2008 at 6:04 AM, SteveC <[email protected]> wrote:
> 
> > With .NET Framework 3.5, what is the best/fastest/slickest way to
> make a
> > (non-reference) copy of the object of a Plain Old C Sharp Object?
> >
> >
> >
> > //My Sample
> >
> > public class MyClass
> >
> > {
> >
> >    private string _mystring;
> >
> >
> >
> >    public string MyString
> >
> >    {
> >
> >        Get { return _mystring }
> >
> >        Set { _mystring = value}
> >
> >    }
> >
> >
> >
> >    public int IntWithNoPriv { get; set; }
> >
> >
> >
> > }
> >
> >
> >
> >
> >
> > Thanks.
> >
> >
> > ===================================
> > View archives and manage your subscription(s) at
> > http://peach.ease.lsoft.com/archives
> >
> 
> ===================================
> View archives and manage your subscription(s) at
> http://peach.ease.lsoft.com/archives

===================================
View archives and manage your subscription(s) at http://peach.ease.lsoft.com/archives
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.