简而言之,关于 Func<> 可以说些什么?

发布于 2024-08-30 00:30:02 字数 290 浏览 7 评论 0 原文

我已经看到 Func 一段时间了,并且我已经设法避免它(暂时)。但现在看来,我是无法永远躲开了。例如,我尝试了 Dynamic Linq,但几乎所有内容都是根据 Func<> 进行的。我已经尝试过我的一本书(C# 2008/Deitel&Deitel)以及 MSDN,但我还没有得到它。他们都直接切入主题。

  1. 关于 Func<> 可以说些什么(用几句话来说)
  2. 我可以在网上找到一些可以帮助我开始处理此事的链接吗?

感谢您的帮助

I've been seing Func<> for sometime now, and I've manage to avoid it (for now). But, now it looks like I can't dodge it forever. For instance, I tried Dynamic Linq, but almost everything was in terms of Func<>. I've tried one of my book (C# 2008/Deitel&Deitel) and also MSDN but I'm not getting it yet. They all jump straight in the subject.

  1. What can be said (in few words) about Func<>
  2. Can I get some links on the web that can get me started on that matter?

Thanks for helping

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(7

只等公子 2024-09-06 00:30:02

Func<> 是一个通用委托 - 使用起来非常方便,因为您不必为每个参数/返回类型组合创建自己的委托。
早些时候,您必须编写如下内容:

public delegate long MyDelegate( int number );

public void Method( IEnumerable<int> list, MyDelegate myDelegate )
{
    foreach( var number in list )
    {
        myDelegate( number );
    }
}

您必须发布委托,以便用户可以正确调用您的方法。特别是当您需要一堆不同的委托时,您最终会为每个参数列表和返回类型发布一个委托。
使用 Func<> 您只需编写:

public void Method( IEnumerable<int> list, Func<int, long> myDelegate )
{
    foreach( var number in list )
    {
        myDelegate( number );
    }
}

它的含义与第一个代码示例相同 - Func 定义一个委托,该委托接受一个整数参数并返回一个长值。

当然,您也可以使用更长的参数列表:Func 仍然会返回一个 long 值,而它需要两个 int< /em> 和一个 bool 值。如果您希望委托没有返回值,则必须使用 Action<>,它将 void 作为返回类型。

编辑(根据请求):如何调用我的示例中的方法

对于调用者来说,使用 MyDelegate功能。在这两种情况下,他都有三个选项来调用方法:

使用 lambda 表示法(需要 C# 3.0,可能是短方法的最佳解决方案):

Method( myList, i => i * i );

使用匿名方法(需要 C# 2.0):

Method( myList, delegate( int i )
{
    return i * i;
} );

或者使用真实方法作为参数:

Method( myList, Square );

private static long Square( int number )
{
    return number * number;
}

Func<> is a generic delegate - it is just very convenient to use, because you don't have to create your own delegate for each argument/return type combination.
Earlier, you had to write something like:

public delegate long MyDelegate( int number );

public void Method( IEnumerable<int> list, MyDelegate myDelegate )
{
    foreach( var number in list )
    {
        myDelegate( number );
    }
}

You had to publish your delegate so that a user can call your method correctly. Especially when you need a bunch of different delegates you ended up publishing one for every argument list and return type.
With Func<> you just write:

public void Method( IEnumerable<int> list, Func<int, long> myDelegate )
{
    foreach( var number in list )
    {
        myDelegate( number );
    }
}

It means the same as the first code example - Func<int, long> defines a delegate that takes one integer argument and returns a long value.

Of course you can use longer parameter lists, too: Func<int, int, bool, long> will still return a long value while it takes two ints and a bool value. If you wish a delegate without return value you will have to use Action<>, which will have void as a return type.

EDIT (by request): How to call the method in my example:

For the caller, there is no difference between the solution with MyDelegate or Func<>. In both cases he has three options to call the method:

Using a lambda notation (C# 3.0 required, probably the best solution for short methods):

Method( myList, i => i * i );

By using an anonymous method (C# 2.0 required):

Method( myList, delegate( int i )
{
    return i * i;
} );

Or by using a real method as an argument:

Method( myList, Square );

private static long Square( int number )
{
    return number * number;
}
樱娆 2024-09-06 00:30:02

Func<...> 是委托类型的家族,它返回一些值,并接受一定数量的参数;例如:

  • Func 只是接受一个 int 并返回一个 bool 的东西(返回总是在最后);例如谓词:

    int[] data = {1,2,3,4,5};
    var odd = data.Where(i => i % 2 == 0);
    
  • Func 是返回字符串的方法,例如 () =>; "hello world";.

  • Func 可能类似于 (when,howLong) =>; when + howLong;

同样,还有 Action<...> ,它执行相同的操作,但没有返回类型。

Func<...> 没有什么神奇之处 - 它只是表达委托的一种更简单的方式,而 a:使用泛型(对 LINQ 有用),或 b:不需要您查找什么论据是;如果委托类型是模糊的(例如 PipeStreamImpersonationWorker ),则可能很难知道它需要什么;如果将其表示为类似的 Action ,那么很明显它不带任何参数并返回 void

Func<...> is a family of delegate types, that return some value, and take some number of arguments; for example:

  • Func<int,bool> is simply something that takes an int and returns a bool (the return is always at the end); for example a predicate:

    int[] data = {1,2,3,4,5};
    var odd = data.Where(i => i % 2 == 0);
    
  • Func<string> is a method that returns string, such as () => "hello world";.

  • Func<DateDtime, TimeSpan, DateTime> might be something like (when,howLong) => when + howLong;

Likewise there is Action<...> which does the same but without a return type.

There's nothing magic about Func<...> - it is just a simpler way of expressing delegates, while a: using generics (useful for LINQ), or b: not needing you to look up what the arguments are; if the delegate type is something obscure (PipeStreamImpersonationWorker for example) it can be hard to know what it takes; if that was expressed as the comparable Action it would be clear that it takes no parameters and returns void.

无戏配角 2024-09-06 00:30:02

这可能会有所帮助。假设每次看到 Func 时你都会想:

interface IFuncIntString
{
    string Invoke(int x);
}

也就是说,委托是一个实现了这个接口的对象。它有一个名为 Invoke 的方法,它接受一个 int 并返回一个字符串。

现在添加一个功能,您可以在呼叫中省略“调用”,这样您就拥有了一个代理。

This might help. Suppose every time you see Func<int, string> you think to yourself:

interface IFuncIntString
{
    string Invoke(int x);
}

That is, the delegate is an object that implements this interface. It has a single method called Invoke which takes an int and returns a string.

Now add to that the feature that you can omit the "Invoke" on a call, and you've got yourself a delegate.

耳钉梦 2024-09-06 00:30:02

Func(例如)是一种类型(就像 string 是一种类型一样)。所以你用它来声明变量、字段、参数等等。

它表示每当您询问答案时都可以完成的计算:

Func<int> f = () => DateTime.Now.Second;

// elsewhere...

Console.WriteLine( f() );

请注意如何像调用方法一样调用它。 Func 有许多重载版本来支持不同数量的参数。最后一个类型参数是返回类型。

Func<int, string> quoteInt = n => "\"" + n + "\"";

Console.WriteLine( quoteInt(3) );

Func 是委托类型。您可以声明自己的,但使用 Func 更容易。在要返回 void 的位置,请使用 Action 而不是 Func。仅当需要 outref 参数时,才需要声明自定义委托。

将 lambda 分配给 Func 时,您可以引用局部变量。这是极其强大的;这意味着 Func 不仅仅是代码;它有数据。因此,它就像一个具有单个方法的对象(从技术上讲,该方法称为“Invoke”,并且当您调用委托时,编译器会隐式地为您调用该方法)。

语法 () => 可以放在任何表达式之前,表示“现在不要执行此操作,推迟到稍后执行”。它允许您初始化捕获延迟计算的委托。然后可以将语法 () 放在委托后面以实际触发计算。所以后缀 () 与前缀 () => 相反。

Func<int> (for example) is a type (in the way that string is a type). So you use it to declare variables, fields, parameters and so on.

It represents a computation that can be done whenever you ask it for an answer:

Func<int> f = () => DateTime.Now.Second;

// elsewhere...

Console.WriteLine( f() );

Note how you can call it just like a method. There are many overloaded versions of Func to support different numbers of parameters. The last type argument is the return type.

Func<int, string> quoteInt = n => "\"" + n + "\"";

Console.WriteLine( quoteInt(3) );

Func is a delegate type. You can declare your own, but it's easier to use Func. Where you want to return void, use Action instead of Func. You only need to declare custom delegates if you need out or ref parameters.

When assigning a lambda to a Func, you can refer to local variables. This is extremely powerful; it means that a Func is more than just code; it has data. So it's like an object with a single method (which technically it is - the method is called Invoke and the compiler implicitly calls that method for you when you call a delegate).

The syntax () => can be placed before any expression to say "don't do this now, delay it until later". It allows you to initialize a delegate capturing the delayed computation. And then the syntax () can be placed after the delegate to actually trigger the computation. So suffix () is kind-of the opposite of prefix () =>.

吻安 2024-09-06 00:30:02

您可以从101 Linq 示例开始。

简而言之,Func<> 是一个委托,其中最后一个类型参数是返回类型。

因此,Func 是一个接受 int 参数并返回 bool 的委托。

You can start with 101 Linq Samples.

In short, Func<> is a delegate where the last type parameter is the return type.

So, Func<int,bool> is a delegate that takes an int parameter and returns a bool.

枕头说它不想醒 2024-09-06 00:30:02

函数<..., T>是代表。
其中 T 是返回类型,所有其他 - 输入参数。

Func<..., T> is delegate.
where T is return-type, and all others - input parameters.

坚持沉默 2024-09-06 00:30:02

如果您曾经使用过=>; C# 中的运算符,您可能已经使用过 Funcs。您只是没有明确声明它们。

因此,如果您编写一条语句,就像

var peopleWhoLikeBlue = people.Where(person => person.FavoriteColor == "Blue");

Func 传递到Where() 方法中一样。

如果你想啰嗦一些,你可以像这样重写该语句:

Func<Person, bool> favoriteColorIsBlue = person => person.FavoriteColor == "Blue";
var peopleWhoLikeBlue = people.Where(favoriteColorIsBlue);

你会得到相同的结果。

If you've ever used the => operator in c#, and you probably have, you've already used Funcs. You just haven't been explicitly declaring them.

So, if you write a statement like

var peopleWhoLikeBlue = people.Where(person => person.FavoriteColor == "Blue");

you're passing a Func<Person, bool> into the Where() method.

If you want to be wordy, you can rewrite that statement like this:

Func<Person, bool> favoriteColorIsBlue = person => person.FavoriteColor == "Blue";
var peopleWhoLikeBlue = people.Where(favoriteColorIsBlue);

And you'll get the same result.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文