开发者

Using a new statement in the control part of the for loop

What's the verdict开发者_如何学JAVA on replacing:

Regex exp = new Regex(MyReg, RegexOptions.IgnoreCase);
var matches = exp.Matches(source);
foreach (var m in matches)
      ...

With:

foreach (var m in new Regex(MyReg, RegexOptions.IgnoreCase).Matches(source))
      ...

Performance issues, slower, unreadable? Or OK?


I prefer your first version for readability. Your second version puts too much on one line and I find that quite difficult to work out what is going on.

Also, if you have an error somewhere in this, it is much easier to debug with the first version.


I wouldn’t bother with constructing the redundant Regex object in the first place. Just use the static method:

foreach (var m in Regex.Matches(source, MyReg, RegexOptions.IgnoreCase))
    // ...

This is both the fastest and the most readable that it will get.


Overall performance should be the same. Readability suffers a little IMO, but it's not too bad really. So I would, subjectively, say that it's ok.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜