当您调用一个Action<T>时,您将传入一个类型为T的变量,该变量可用于在委托中定义的代码。
var myAction = new Action<string>(param =>
{
Console.WriteLine("This is my param: '{0}'.", param);
});
myAction("Foo");
// Outputs: This is my param: 'Foo'.当您调用一个Func<T>时,委托将返回一个类型为T的变量。
var myFunc = new Func<string>(() =>
{
return "Bar";
});
Console.WriteLine("This was returned from myFunc: '{0}'.", myFunc());
// Outputs: This was returned from myFunc: 'Bar'.,这里有个问题,-
是否有第三种委托类型,它将接受输入参数并返回值?就像-
var fooDeletegate = new FooDelegate<string, int>(theInputInt =>
{
return "The Answer to the Ultimate Question of Life, the Universe, and Everything is " + theInputInt;
});
Console.WriteLine(fooDeletegate(42));
// Outputs: The Answer to the Ultimate Question of Life, the Universe, and Everything is 42如果这样的东西不存在,是否可以使用Action<Func<sting>>来实现这种功能?
发布于 2011-03-03 15:30:18
您正在寻找Func,或其其他重载之一。
发布于 2011-03-03 15:31:10
存在Func<>过载,参数超过零参数的Func<TParam, TReturn>、Func<TParam1, TParam2, TReturn>等。
发布于 2011-03-03 15:31:24
您可以使用new Func<inputType1, inputType2, inputType3, outputType>()来完成这个任务。在0到16个输入参数下,这是可能的。您将在Func中找到不同的系统命名空间重载。
https://stackoverflow.com/questions/5182797
复制相似问题