简体   繁体   中英

Check of two list have colliding element?

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)
    {

.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.

Eg

if(list1.Intersect(list2).Any()) 

你总是可以使用linq

if (list1.Intersect(list2).Count() > 0) ...

如果你能够使用Linq那么if(list1.Intersect(list2).Count > 0) {...collision...}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM