Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C# - foreach loop - Good Practice

Is this good or bad:

foreach (Match match in serverNameRegex.Matches(loginPage))
{
 ....
}

or should I use it like that for better speed:

MatchCollection matches = serverNameRegex.Matches(loginPage);
foreach (Match match in matches)
{
  ...            
} 
like image 205
Hooch Avatar asked Dec 07 '22 17:12

Hooch


1 Answers

As long as the MatchCollection cannot be null, I'd say it's your pick.

But if it turns out null and you don't check it beforehand, you will run into a NullReferenceException.

like image 51
Timothée Bourguignon Avatar answered Dec 18 '22 04:12

Timothée Bourguignon