Forum Xamarin.Forms
We are excited to announce that the Xamarin Forums are moving to the new Microsoft Q&A experience. Q&A is the home for technical questions and answers at across all products at Microsoft now including Xamarin!

We encourage you to head over to Microsoft Q&A for .NET for posting new questions and get involved today.

How to clone a list

AlessandroCaliaroAlessandroCaliaro ITMember ✭✭✭✭✭

Hi. Sorry for the 'not XF' question but I don't know where is the right place for this.
I would like to know how to clone a list so I create a second list that not reference the first one.
If I have
List l1 = l2;

And if I modify l1[0].myvalue = 5
The same value is assigned to l2[0].myvalue

how can I have two separate list? I have not found a Clone method...

Best Answers

Answers

  • adamkempadamkemp USInsider, Developer Group Leader mod
    var newList = new List<MyType>(oldList);
    
  • AlessandroCaliaroAlessandroCaliaro ITMember ✭✭✭✭✭

    Adam I have already tried your suggestion but if I change a value in newList , I see the value changes also in oldList... I try again. Now I am at deep Purple concert...

  • Amar_BaitAmar_Bait DZMember ✭✭✭✭✭
    edited October 2015

    Because your objects in the list are classes, so your list holds references to your object. In OO programming in general, there are two types of objects (I'm using simplified words here): reference type and value types. Let's take this example:

    var number1 = 5;
    var number2 = 3;
    var number1bis = number1        // 5;    
    

    now if you change number1bis, number1 doesn't change, because when you assigned number1 to number1bis, you assigned its value, not the reference of the variable itself. So if you do number1bis = 6; number1 doesn't change (still 5).

    Those are called value types and are primitives objects like int, string, double, structures...etc

    For classes ("complex" objects) it's different:

    var cat1 = new Cat();
    var cat2 = new Cat();
    var cat1bis = cat1;
    

    Now if you do:

    cat1bis.Name = "Cute";
    

    Both cat1 and *cat1bis will have their names "Cute". Why? Because they're the same object! when you assigned cat1 to cat1bis, it didn't make a clone of it, it just pointed to it (like a shortcut): that's reference type.

    So now what if you want to make a copy of 1st cat, but independent of the first one? that's what we call cloning. The simplest method to do it is to create a new Cat then copy properties from first Cat to 2nd one, because Cat properties are value types (Name string, age int....etc) you got it?

    So to resume your example, when you have a List() what you really have is just a list of references to the objects, not the objects themselves inside a list, so when you modify an object inside that list, it'll be changed for all lists containing that object, because they all point to the same object.

    I hope I was clear enough,

  • AlessandroCaliaroAlessandroCaliaro ITMember ✭✭✭✭✭

    Thanks @Nadjib.Bait , it's clear. The problem is that I haven't a Clone method, so I should copy all values one to one or use something like reflection... Or there is some simplest solution?

  • adamkempadamkemp USInsider, Developer Group Leader mod

    If that's what you need then you should make your own Clone method. There isn't one built in because there's no way to know in general for every class how to copy it correctly.

Sign In or Register to comment.