简体   繁体   中英

List.Contains returns incorrect result but List.Exists works

I have a list of COM objects. When I check whether a given COM object exists in the list, the contains method returns incorrect result sometimes, while exists always works. Any idea why this might be happening?

bool CheckContains(List<object> objectList, object target){
     //return objectList.Contains(target); Gives incorrect results sometimes
     return objectList.Exists(obj=>obj==target); //Always works
}

convert from the non-generic to generic using Cast or OfType try this,

bool CheckContains(nonGeneric.Cast(IEnumerable<object>     objectList)).Contains(target){
        return objectList.Contains(target);
   }

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