Question

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();
Était-ce utile?

La solution 3

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

}

Autres conseils

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

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();

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

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.

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top