简体   繁体   中英

C# - delegate System.Func< >

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

simple example would be helpful

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:

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 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.

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

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.

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:

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:

var stringsStartingWithR = strings.Where(s => s.StartsWith("R"));
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;
}

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.

using System; class Program { 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());    } }

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM