Search Unity

List.equals does not work

Discussion in 'Scripting' started by jimma, Feb 7, 2014.

  1. jimma

    jimma

    Joined:
    Apr 3, 2013
    Posts:
    28
    Hello,

    I am trying to implement a simple alogrithm in my game where I need to use lists.

    I am not able to fetch the right output comparing two lists for equality.
    Code (csharp):
    1.  
    2. List<string> l1 = new List<string>();
    3. List<string> l2 = new List<string>();
    4.  l1.Add ("Red");
    5. l1.Add ("Blue");
    6. l2.Add ("Red");
    7. l2.Add ("Blue");
    8. print (l1.Equals (l2));
    result i get here is false! any ideas?
     
  2. GibTreaty

    GibTreaty

    Joined:
    Aug 25, 2010
    Posts:
    792
    http://stackoverflow.com/questions/...c-sharp-net-not-working-when-using-nhibernate

    If you're open to using the Linq library here are a couple things you could use.

    Intersect
    Gets which items are the same in both lists

    Except
    Gets which items aren't the same in both lists

    Then you could compare the count of the intersected/excepted list to the original list count.
     
    Last edited: Feb 7, 2014
  3. jimma

    jimma

    Joined:
    Apr 3, 2013
    Posts:
    28
    Thank you so much. Got it working now.

    Just so that I am not confused in the future, list.equals does give out if two list are equal in elements and size right.. in general outside of Unity because I remember using that in Java. It won't work in Unity for the reasons you explained. Thanks!!