1

I have a problem with a list and I want to make a copy of the original list, because if I change a value in the list copy modifies the value of the original, the list is of type detail of a bill as I tried it following, but does not work.

List<detail> detail = new List<detail>(); 
detail = saleController.getAll();
List<detail> copyDetail = new List<detail>(detail);

I appreciate the help my problem

svick
  • 236,525
  • 50
  • 385
  • 514
baxi2990
  • 13
  • 4
  • I think I get your problem now. When you say 'change a value in the list copy`, you mean change a value _within an object_ in the list copy, right? – SF Lee May 06 '14 at 00:54
  • My class have attributes as price, quality, sale, product then I need to change an attribute of my list copy – baxi2990 May 06 '14 at 06:47

4 Answers4

3

For each object in the collection to be copied, you can copy it and add it to the destination collection. How to copy that instance likely depends on the instance type being copied. I.e does that class have a copy constructor?

jordan
  • 959
  • 7
  • 17
3

It sounds like detail is a reference type. When you duplicate the list, each list (whose backing store is an array) contains references to the same set of detail instances.

You need to make a deep copy or clone.

If detail implements ICloneable it's easy:

List<detail> original = GetListOfDetails() ;
List<detail> clone    = original.Select( x => x.Clone() ).Cast<detail>().ToList() ;

However, you're at the mercy of the semantics of detail.Clone(): there's no guarantee that it, actually performs a deep copy. See MemberwiseClone() for some suggestions on how you might implement a correct deep copy.

If detail is serializable, that is, the class has the attribute [Serializable] or it implements ISerializable, the easiest and simplest (though neither elegant nor particular fast) way would be to serialize it to a stream and rehydrate it as a new list, something like this:

public static IEnumerable<T> Clone<T>( this IEnumerable<T> list )
{
  BinaryFormatter serializer = new BinaryFormatter() ;

  using ( MemoryStream stream = new MemoryStream() )
  {
    foreach( T item in list )
    {

      // serialize
      serializer.Serialize(stream,item) ;
      stream.Flush() ; // probably unneeded for a memory stream, but belts-and-suspenders, right?

      // rewind and rehydrate
      stream.Seek(0,SeekOrigin.Begin) ;
      T clone = (T) serializer.Deserialize( stream ) ;

      // rewind and clear the memory stream
      stream.Seek(0,SeekOrigin.Begin) ;
      stream.SetLength(0) ;

      yield return clone ;
    }
  }
}

...

List<detail> original = GetListOfDetails() ;
List<detail> clone    = original.Clone().ToList() ;
Nicholas Carey
  • 71,308
  • 16
  • 93
  • 135
  • Try to implement your solution but still did not work, I will comment like this structured my project, I have classes for sale, products and detail that is the many to many relationship in my class product sale. It is noteworthy that use nhibernate sale and in my class there is a detailed list, then I do not know where to implement the method, I appreciate the help. – baxi2990 May 06 '14 at 06:24
0
List<detail> detail = new List<detail>();
detail = saleController.getAll();
List<detail> copyDetail = new List<detail>();
copyDetail = detail.Clone();
Rachit Patel
  • 854
  • 5
  • 12
0

Cloning an Object like that will modify the Original object. You have to copy the values of the original list to another one using somthin like ForeEach:

private void btnCopy_Click(object sender, EventArgs e)
    {
        List<detail> detail = new List<detail>();
        List<detail> CopyDetail = new List<detail>();
        detail.Add(new detail{item1=1,item2=1});
        foreach (detail item in detail)
        {
            CopyDetail.Add(new detail{item1=item.item1,item2=item.item2});
        }

    }


public class detail

{
    public int item1;
    public int item2;
}
Ghasem
  • 14,455
  • 21
  • 138
  • 171