我有代码:
public void FindMatches(string source)
{
...
var matchCollections = new List<MatchCollection>();
Parallel.ForEach(patterns,
pattern =>
{
var regex = new Regex(pattern);
MatchCollection matches = regex.Matches(source, 0);
matchCollections.Add(matches);
}
);
foreach (MatchCollection matches in matchCollections)
{
if (matches.Count > 0) //NullReferenceException
{
foreach (Match match in matches)
{
...
}
}
}
...
}
有时我在第 15 行遇到 NullreferenceException。如果在插入“matchCollections”之前检查“matches”不为空,则仍然会抛出异常。有什么问题吗?