考虑以下几点:LINQ和递归
public class Box
{
public BoxSize Size { get; set; }
public IEnumerable<Box> Contents { get; set; }
}
Box FindBoxBySize(Box box, BoxSize size)
{
Box _foundBox = null;
Action<IEnumerable<Box>> _recurse = null;
_recurse = new Action<IEnumerable<Box>>(boxes =>
{
foreach (var _box in boxes)
{
if (_box.Size == size)
{
_foundBox = _box;
return;
}
if (_box.Contents != null) _recurse(_box.Contents);
}
});
_recurse(box.Contents);
return _foundBox;
}
有没有什么办法可以FindBoxBySize()
使用LINQ被压缩?另外:欢迎对我的代码发表评论。我没有做太多的递归,所以我可能在我的实现中遗漏了一些东西。
我认为它的确如此。但请注意我的诀窍;我真的很喜欢那里尽可能少的“循环”,所以无论何时找到一场比赛,我想终止。 – roosteronacid 2010-11-23 14:49:00