我需要幫助最小化代碼。我必須檢查兩個不同的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