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