C# - 委托 System.Func< >

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1313757/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-06 15:00:53  来源:igfitidea点击:

C# - delegate System.Func< >

c#

提问by user160677

How to use delegate System.Func< >? Shall we control the execution order of funcion or events using it?

如何使用委托 System.Func< >?我们应该使用它来控制函数或事件的执行顺序吗?

simple example would be helpful

简单的例子会有所帮助

采纳答案by LorenVS

Suppose you have a function such as:

假设您有一个函数,例如:

private static string toLower(string s)
{
    return s.ToLower();
}

There is a version of System.Func that takes two generic arguments, the first being the type of the first parameter, the second being the return type. As such, you could write:

有一个 System.Func 版本接受两个泛型参数,第一个是第一个参数的类型,第二个是返回类型。因此,您可以编写:

Func<string,string> myFunction = toLower;
string s = myFunction("AsDf"); 
// s is now "asdf"

In all versions of System.Func, the last generic argument is the return type, all the others are the types of the parameters, in order.

在 System.Func 的所有版本中,最后一个泛型参数是返回类型,所有其他参数都是参数的类型,按顺序排列。

System.Func is usefull because it does not require you to write custom delegate types. This makes it much easier to interop delegates with the same signature.

System.Func 很有用,因为它不需要您编写自定义委托类型。这使得具有相同签名的互操作委托变得更加容易。

Say I had:

说我有:

public delegate string MyDelegate1(string s);
public delegate string MyDelegate2(string s);

MyDelegate1 myDel = new MyDelegate1(toLower); // toLower as above

There is now no way to conver my MyDelegate1 delegate to an object of type MyDelegate2, even though they have the same method signature. On the other hand, if we had used Func instead of declaring a custom delegate type, we wouldn't have this problem

现在无法将 MyDelegate1 委托转换为 MyDelegate2 类型的对象,即使它们具有相同的方法签名。另一方面,如果我们使用 Func 而不是声明自定义委托类型,我们就不会有这个问题

回答by Reed Copsey

System.Func<T>is usually used as an argument to another function. It can be any delegate that returns a value of T - and there are multiple versions for using as a delegate with multiple arguments.

System.Func<T>通常用作另一个函数的参数。它可以是任何返回 T 值的委托 - 并且有多个版本可用作具有多个参数的委托。

One common usage is for filtering - for example, in LINQ, you can pass a function to use as a filter in the Enumerable.Where function, to restrict a collection. For example:

一种常见用法是用于过滤 - 例如,在 LINQ 中,您可以传递一个函数以用作 Enumerable.Where 函数中的过滤器,以限制集合。例如:

public bool FilterByName(string value)
{
    return value.StartsWith("R");
}

// .. later

List<string> strings = new List<string> { "Reed", "Fred", "Sam" };

var stringsStartingWithR = strings.Where(FilterByName);

However, in the above case, you'd more likely use lambda expressions to build the Func<string,bool>on the fly, as:

但是,在上述情况下,您更有可能使用 lambda 表达式来Func<string,bool>即时构建,如下所示:

var stringsStartingWithR = strings.Where(s => s.StartsWith("R"));

回答by J. Random Coder

void Foo()
{
    Func<object, bool> func = Bar;
    bool b1 = func(new object()); // b1 is true
    bool b2 = func(null); // b2 is false
}

bool Bar(object o)
{
    return o == null;
}

回答by jagprinderdeep

Use it to represent any delegate type that has a return value.

使用它来表示具有返回值的任何委托类型。

回答by lipika chakraborty

1)Create a Func instance that has one parameter and one return value. 2)Func instance with two parameters and one result. Receives bool and int, returns string. 3)Func instance that has no parameters and one result value. 4) Call the Invoke instance method on the anonymous functions.

1) 创建一个具有一个参数和一个返回值的 Func 实例。2)具有两个参数和一个结果的Func实例。接收 bool 和 int,返回字符串。3)没有参数和一个结果值的Func实例。4) 对匿名函数调用 Invoke 实例方法。

using System; class Program { static void Main() {

使用系统;类程序{ static void Main() {

Func<int, string> func1 = (x) => string.Format("string = {0}", x);  
Func<bool, int, string> func2 = (b, x) =>
    string.Format("string = {0} and {1}", b, x);

Func<double> func3 = () => Math.PI / 2;

Console.WriteLine(func1.Invoke(10));
Console.WriteLine(func2.Invoke(true, 20));
Console.WriteLine(func3.Invoke());    } }