C# 中的匿名方法可以调用自身吗? [英] Can an anonymous method in C# call itself?
本文介绍了C# 中的匿名方法可以调用自身吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有以下代码:
class myClass
{
private delegate string myDelegate(Object bj);
protected void method()
{
myDelegate build = delegate(Object bj)
{
var letters= string.Empty;
if (someCondition)
return build(some_obj); //This line seems to choke the compiler
else string.Empty;
};
......
}
}
是否有另一种方法可以在 C# 中设置匿名方法,使其可以调用自身?
Is there another way to set up an anonymous method in C# such that it can call itself?
推荐答案
你可以把它分解成两条语句,利用捕获变量的魔力来达到递归的效果:
You can break it down into two statements and use the magic of captured variables to achieve the recursion effect:
myDelegate build = null;
build = delegate(Object bj)
{
var letters= string.Empty;
if (someCondition)
return build(some_obj);
else string.Empty;
};
这篇关于C# 中的匿名方法可以调用自身吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文