我需要帮助最小化代码。我必须检查两个不同的maches,并且需要将它们存储在同一个matchcollection中。我不知道该怎么做,这里是我的代码,请任何人都帮助我这么做。如何创建matchcollection数组
var patternEmail = @"\w+([-+.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*";
MatchCollection emailCollection1;
//Get emails from ResponsibleConsultant
emailCollection1 = Regex.Matches(piWorkitem.ResponsibleConsultant, patternEmail);
foreach (Match mail in emailCollection1.Cast<Match>().Where(mail => !emailaddresses.Contains(mail.Value.ToString())))
{
emailaddresses.Add(mail.Value);
}
MatchCollection emailCollection2;
//Get emails from ResponsibleConsultant
emailCollection2 = Regex.Matches(piWorkitem.SupplierConsultant, patternEmail);
foreach (Match mail in emailCollection2.Cast<Match>().Where(mail => !emailaddresses.Contains(mail.Value.ToString())))
{
emailaddresses.Add(mail.Value);
}
帮助我避免重复代码不止一次。
使包含逻辑函数,然后重新使用该功能。 – Raptor 2013-02-20 06:04:46