2013-01-19 57 views
2

我找到了this post,它解释了如何在C#中将方法作为参数传递。从C#方法返回方法

我需要知道的是如何返回一个方法作为另一个方法调用的结果。

method = DoSomething() 
result = method() 
+0

只需返回一个Func委托。 –

回答

4

你需要为使用Action<T>Func<T>

像这样:

private Action<string> Returns(string user) 
{ 
    return() => 
    { 
     Console.WriteLine("Hey {0}", user); 
    }; 
} 

或本:

private Func<bool> TestsIsThirty(int value) 
{ 
    return() => value == 30; 
} 
2

很可能你希望你的返回类型是Delegate

2
var method =()=> DoSomething(); 
result = method();