У меня есть код:NullReferenceException использованием Parallel.ForEach для Regex.Matches
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)
{
...
}
}
}
...
}
Иногда я получил NullReferenceException в 15 строке. В случае проверки «совпадений» не имеет значения null до вставки в «matchCollections» - исключение все равно бросается. В чем проблема?