开发者

Recursive call - Action lambda

What am I doing wrong here? How can I execute my action?

var recurse = new Action<IItem, Int32>((item, depth) =>
{
    if (item.Items.Count() > 0) recurse(item, dept开发者_C百科h + 1); // red squiggly here

    // ...
});

I'm getting a red squiggly when calling recurse saying "method, delegate or event expected".

Update

I've accepted Homam's answer. I'd just like to add/share another syntax for the same... But which I find a bit easier on the eyes...

Action<IEnumerable<Item>> Recurse = null;

Recurse = item =>
{
    if (item.Items != null) Recurse(item.Items);

    // ...
};


Just define the delegate Action and assign null to it before calling it recursively.

Action<IItem, Int32> recurse = null;

Then

recurse = new Action<IItem, Int32>((item, depth ) =>
{
    if (item.Items.Count() > 0) recurse(item, depth + 1); // red squiggly here
    // ...
});

Good luck!

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜