- Home /
List.Contains problem
pipeData.Add(new int[] {firstPoint,tempPoint});
print(pipeData.Contains(new int[] {firstPoint,tempPoint}));
This code returns false. I don't get it. I get no errors.
EDIT: It's in Update()
Answer by robertbu · Feb 20, 2014 at 01:55 AM
I use this area of C# infrequently enough that I stumble through a bit each time. Your 'pipeData' List contains references to the array stored for each element. Used this way, Contains is comparing the references to each other. Your second new is creating a new reference, so the comparison fails. To highlight the issue, run this code:
int[] foo = new int[] {firstPoint,tempPoint};
pipeData.Add(foo);
print(pipeData.Contains(foo));
This will succeed. A couple of possible solutions (and I'm sure there are more):
First, you can place your array in a class that takes the array as a parameter in the constructor. Then you can override the 'Equals' method to do the comparison any way you want. See the List.Contains reference page for an example.
Second, you could use List.Find() instead and provide a predicate. A non-null value means it is found.