C# 递归调用操作lambda
我做错了什么?我如何执行我的操作C# 递归调用操作lambda,c#,recursion,delegates,C#,Recursion,Delegates,我做错了什么?我如何执行我的操作 var recurse = new Action<IItem, Int32>((item, depth) => { if (item.Items.Count() > 0) recurse(item, depth + 1); // red squiggly here // ... }); 在递归调用它之前,只需定义委托操作并为其赋值null即可 Action<IItem, Int32> recurse = n
var recurse = new Action<IItem, Int32>((item, depth) =>
{
if (item.Items.Count() > 0) recurse(item, depth + 1); // red squiggly here
// ...
});
在递归调用它之前,只需定义委托
操作
并为其赋值null即可
Action<IItem, Int32> recurse = null;
动作递归=null;
然后
recurse=新操作((项目,深度)=>
{
如果(item.Items.Count()>0)递归(item,depth+1);//此处红色扭曲
// ...
});
祝你好运 我不想把定义和实现分开。我有没有办法用一行代码就能做到这一点?没有。埃里克在他的博客文章中解释了原因(看起来很正常):@Ron:好消息。有一些奇怪的意义:)有一些方法可以实现匿名递归,比如定义一个Y组合子,但这要简单得多。请参阅,Eric的博客上的评论有一个定点组合器,它允许您在声明点实现lambda,而无需拆分。
Action<IItem, Int32> recurse = null;
recurse = new Action<IItem, Int32>((item, depth ) =>
{
if (item.Items.Count() > 0) recurse(item, depth + 1); // red squiggly here
// ...
});