Re: MemberwiseClone() doesn't like arrays?
- From: "Marc Gravell" <marc.gravell@xxxxxxxxx>
- Date: Thu, 10 Jan 2008 13:50:04 -0000
Easy - it has done a *shallow* clone, probably by a blit; this means
that it has, as promised, copied the member variable exactly; the
clone *also* contains a field that is a reference to the managed
heap - i.e. the same object. This is a common problem with using
shallow clone on an object whose fields are references.
You could try serializiation? This is the simplest approach to get a
deep-clone (assuming your data is serializable).
Or simply implement Clone() yourself, and manually clone the array
(giving the new instance the cloned array). Of course, if the array is
an array of reference-types (Customer, perhaps), then note that now
you have 2 arrays pointing to the same Customer objects, so
original.Data[0].Name = "Fred" will also update
newClone.Data[0].Name...
Do you see the problem?
Marc
.
- Follow-Ups:
- Re: MemberwiseClone() doesn't like arrays?
- From: pinkfloydfan
- Re: MemberwiseClone() doesn't like arrays?
- References:
- MemberwiseClone() doesn't like arrays?
- From: pinkfloydfan
- MemberwiseClone() doesn't like arrays?
- Prev by Date: GPL [1/1] - "gpl.txt" yEnc (1/1)
- Next by Date: Re: Create Access database as fast as possible
- Previous by thread: MemberwiseClone() doesn't like arrays?
- Next by thread: Re: MemberwiseClone() doesn't like arrays?
- Index(es):
Relevant Pages
|