views:

40

answers:

3

Is there a way to check if one list collides with another? ex:

    bool hit=false;
    foreach(var s in list2)
    {
        if (list1.Contains(s))
        {
            hit = true;
            break;
        }
    }
    if (!hit)
    {
+4  A: 

.NET has a number of set operations that work on enumerables, so you could take the set intersection to find members in both lists. Use Any() to find out if the resulting sequence has any entries.

E.g.

if(list1.Intersect(list2).Any()) 
Brian Rasmussen
+1  A: 

If you're able to use Linq then if(list1.Intersect(list2).Count > 0) {...collision...}.

Will A
+3  A: 

You can always use linq

if (list1.Intersect(list2).Count() > 0) ...
Calin
Keep in mind that `Count()` may need to enumerate the entire sequence. Use `Any()` instead.
Brian Rasmussen
Only that does ofcourse not work after `Intersect`'ing.
Dykam