是否有一个委托结合了Func <T>和Action <T>的功能?

jam*_*ind 0 .net c# delegates

当你调用a时,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'.
Run Code Online (Sandbox Code Playgroud)

当你调用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'.
Run Code Online (Sandbox Code Playgroud)

这是问题 -

有这将需要输入参数和第三委托类型返回一个值?就像是 -

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
Run Code Online (Sandbox Code Playgroud)

如果不存在这样的事情,是否可以使用Action<Func<sting>>这种功能?

SLa*_*aks 16

你正在寻找Func<T, TResult>,或其他15重载之一.