Frage

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

War es hilfreich?

Lösung 3

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;
}

Andere Tipps

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?

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() ;
List<detail> detail = new List<detail>();
detail = saleController.getAll();
List<detail> copyDetail = new List<detail>();
copyDetail = detail.Clone();
Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top