[英]List.Exists returns wrong values
I was using a List to store all the devices retrieved using a third party library. 我使用List来存储使用第三方库检索的所有设备。 I am using the Exist method to determine whether a device existing inside a list. 我使用Exist方法来确定列表中是否存在设备。
bool bDeviceFound = _devicesFound.Exists(delegate(RCDevice device)
{
bool retVal = false;
if (device != null)
{
Regex regex = new Regex(@"Floor[\d]+\/mycamera[\d]+");
if (regex.IsMatch(device.FullName))
retVal = true;
}
return retVal;
});
The problem is that delegate never returns true or the execution will never reaches to the code inside the delegate. 问题是委托永远不会返回true,或者执行永远不会到达委托内的代码。 Am I doing anything wrong here? 我在这里做错了吗? I have verified the code inside the delegate and Regular expression returns true whenever a match is found. 我已经验证了委托中的代码,并且只要找到匹配项,正则表达式就会返回true。
device.FullName assumes the value "Floor1/mycamera1" to IsMatch to return true. device.FullName假定值为“Floor1 / mycamera1”,IsMatch返回true。
As suggested by LasseV.Karlsen in one of the comment below , I tried moving the delegate method into a seperate private static bool method and put a break point there. 正如LasseV.Karlsen在下面的评论中所建议的那样,我尝试将委托方法转换为单独的私有静态bool方法并在那里设置一个断点。 but execution never hit there. 但执行永远不会打到那里。 Thanks 谢谢
My guess is the issue lies in this line: 我的猜测是问题在于这一行:
if (regex.IsMatch(rc.FullName))
Why are you checking rc.FullName
? 你为什么要检查rc.FullName
? Shouldn't you be checking device.FullName
instead since device
is what's defined in the delegate? 您是否应该检查device.FullName
因为device
是委托中定义的?
As a side note, you ought to move the Regex
definition outside of the Exists
call for better performance. 作为旁注,您应该在Exists
调用之外移动Regex
定义以获得更好的性能。 Currently it gets recompiled with each iteration of the loop. 目前,它会在循环的每次迭代中重新编译。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.