-3

I try copy a list, but when I change the second list, the first list is changed with the second list.

My model class:

public class urunler : ICloneable
{
public int id { get; set; }
public string icerik { get; set; }
}

Extensions class:

using System.Collections.Generic;
using System;
using System.Linq;

namespace Extensions
{
    public static class Extensions {

        public static IList<T> Clone<T>(this IList<T> SourceList) where T: ICloneable
        {
            return SourceList.Select(item => (T)item.Clone()).ToList();
        }
    }
}

BLL class:

using System.Linq;
using Extensions;
public class bll
{
    public void examp
    {
        List<urunler> L1 = new List<urunler>();
        urunler U = new urunler();
        U.icerik="old";
        L1.Add(U);
        List<urunler> L2 = L1.Clone();
        L2[0].icerik="new";
        MessageBox.show(L1[0].icerik);
        MessageBox.show(L2[0].icerik);
        //
    }
}

Error:

error CS0535: `urunler' does not implement interface member `System.ICloneable.Clone()'

And then I try change model class:

public class urunler : ICloneable
{
    #region ICloneable implementation

    IList<urunler> ICloneable.Clone()
    {
        throw new NotImplementedException ();
    }

    #endregion
    public int id { get; set; }
    public string icerik { get; set; }
}

error:

error CS0539: `System.ICloneable.Clone' in explicit interface declaration is not a member of interface

It works this time, I changed my model class

public class urunler : ICloneable
{
    public object Clone()         
    {             
        return this.MemberwiseClone();         
    }
    public int id { get; set; }
    public string icerik { get; set; }
}

And changed my bll class:

//before:
//List<urunler> L2 = L1.Clone();
//after:
List<urunler> L2 = L1.Clone().toList();
Zong
  • 6,160
  • 5
  • 32
  • 46
  • You are copying references. Use clone method or CopyTo method or ctor with old list list to create an in dependent list based on an original. – icbytes May 12 '14 at 12:43
  • Answered many times http://stackoverflow.com/a/222623/125740 – Yahya May 12 '14 at 12:44
  • Do your research please. – Yahya May 12 '14 at 12:44
  • You have too clone the list. Look at this http://stackoverflow.com/questions/222598/how-do-i-clone-a-generic-list-in-c. – Tan May 12 '14 at 12:45
  • 1
    It's funny that you commented out `//or L2.AddRange(L1);` because that would've worked. – Rik May 12 '14 at 12:50
  • possible duplicate of [How create a new deep copy (clone) of a List?](http://stackoverflow.com/questions/14007405/how-create-a-new-deep-copy-clone-of-a-listt) – huMpty duMpty May 12 '14 at 16:03

5 Answers5

1

You're not copying a list, you're making a new reference to the same list. To copy a List, try this:

List<urunler> L2 = new List<urunler>(L1);
Rik
  • 28,507
  • 14
  • 48
  • 67
1

You can just use ToList() to create a copy / new list

var L2 = L1.ToList();

If you wish to create a new list, and create copies of the individual list-items, you will need to add a 'Clone' method to your object definition:

public class urunler : ICloneable
{
    public Object Clone()
    {
        return this.MemberwiseClone();
    }
}

Then:

var L2 = L1.Select(item => (urunler)item.Clone()).ToList();
Dave Bish
  • 19,263
  • 7
  • 46
  • 63
0

you can write your Extension Method for it:

public static class Extensions
{
    public static IList<T> Clone<T>(this IList<T> SourceList) where T: ICloneable
    {
        return SourceList.Select(item => (T)item.Clone()).ToList();
    }
}

use like this:

List<urunler> L2 = L1.Clone();

Your class:

public class urunler : ICloneable 
{ 
public int id { get; set; } 
public string ismi { get; set; } 
public string icerik { get; set; }

 public object Clone()
    {
        return this.MemberwiseClone();
    } 

}
Ehsan Sajjad
  • 61,834
  • 16
  • 105
  • 160
0

L2=L1 Here's the mistake. L2 is referencing the same List as L1. You need to make a new List and copy the items:

L2 = new List<urunler>();
L1.CopyTo(L1);
//Edit: the above is wrong
L2 = new List<urunler>(L1);
Dennis_E
  • 8,751
  • 23
  • 29
0

Your type urunler appears to be a class, hence a reference type. Can you show us the definition of the urunler class, please?

Therefore, even when you clone the list correctly, you still produce a shallow clone. The two distinct List<> instances will have references to the same instance of urunler.

When you do L2 = L1;, you don't even clone the List<>. You just have two references to the same List<>, so that goes wrong at an early stage.

When you do L2 = new List<urunler>(L1); or L2 = L1.ToList(); or L2 = new List<urunler>(); L2.AddRange(L1);, you get the shallow copy I described. There are two distinct lists of references, but they refer to the same urunler instance.

Consider the cloning solution of the edited version of Dave Bish's answer.

Depending of your semantics, you could also make urunler an immutable type. Then instead of stuff like:

L2[0].icerik = "new";

you would have to do:

L2[0] = new urunler { icerik = "new", OtherProp = L2[0].OtherProp, };

etc.

Jeppe Stig Nielsen
  • 60,409
  • 11
  • 110
  • 181