0

I have a two list objects, and I'm trying to copy the value of one list to the other. Ideal coding would be this:

            List<List<int>> x = new List<List<int>>();
            List<int> temp = new List<int>();
            List<List<int>> y = new List<List<int>>();

            int myCount;


                foreach (List<int> t in y)
                {
                    myCount = t.Count;
                    for(int w = 0; w <= t.Count; w++)
                    {
                        temp = new List<int>();
                        temp = t;
                        temp.Insert(w,n);
                        x.Add(temp);
                    }
                }

Clearly, temp = t; won't be a value copy, but what's the easiest way to make this assignment a value copy instead of a ref copy.

I tried t.ToList(), but intellisense doesn't see that (nor is it listed in the List definition on the MSN help page). I tried "MemberwiseClone" but this also isn't exposed in intellisense. It's times like these where I miss pointers... :)

tmptplayer
  • 434
  • 7
  • 18

1 Answers1

2
 public static T DeepClone<T>(T obj)
    {
        using (var ms = new MemoryStream())
        {
            var formatter = new BinaryFormatter();
            formatter.Serialize(ms, obj);
            ms.Position = 0;

            return (T)formatter.Deserialize(ms);
        }
    }
temp = DeepClone(t);
Ayan_84
  • 645
  • 1
  • 6
  • 18