首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >是否有将Func<T>和Action<T>的功能结合起来的委托?

是否有将Func<T>和Action<T>的功能结合起来的委托?
EN

Stack Overflow用户
提问于 2011-03-03 15:28:56
回答 3查看 256关注 0票数 0

当您调用一个Action<T>时,您将传入一个类型为T的变量,该变量可用于在委托中定义的代码。

代码语言:javascript
复制
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的变量。

代码语言:javascript
复制
var myFunc = new Func<string>(() =>
{
    return "Bar";
});

Console.WriteLine("This was returned from myFunc: '{0}'.", myFunc());

// Outputs: This was returned from myFunc: 'Bar'.

,这里有个问题,-

是否有第三种委托类型,它将接受输入参数并返回值?就像-

代码语言:javascript
复制
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>>来实现这种功能?

EN

回答 3

Stack Overflow用户

回答已采纳

发布于 2011-03-03 15:30:18

您正在寻找Func,或其其他重载之一。

票数 16
EN

Stack Overflow用户

发布于 2011-03-03 15:31:10

存在Func<>过载,参数超过零参数的Func<TParam, TReturn>Func<TParam1, TParam2, TReturn>等。

票数 2
EN

Stack Overflow用户

发布于 2011-03-03 15:31:24

您可以使用new Func<inputType1, inputType2, inputType3, outputType>()来完成这个任务。在0到16个输入参数下,这是可能的。您将在Func中找到不同的系统命名空间重载。

票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/5182797

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档