代码之家  ›  专栏  ›  技术社区  ›  cllpse

递归调用-操作lambda

  •  33
  • cllpse  · 技术社区  · 14 年前

    我在这里做错什么了?如何执行我的操作?

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

    打电话的时候我浑身都红了 recurse 应为方法、委托或事件 .


    更新

    我接受了霍曼的回答。我只想添加/共享相同的另一个语法…但我觉得眼睛比较容易…

    Action<IEnumerable<Item>> Recurse = null;
    
    Recurse = item =>
    {
        if (item.Items != null) Recurse(item.Items);
    
        // ...
    };
    
    1 回复  |  直到 13 年前
        1
  •  54
  •   Homam    14 年前

    Action

    Action<IItem, Int32> recurse = null;
    

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