简而言之,关于Func的说法是什么? [英] In few words, what can be said about Func<>

查看:81
本文介绍了简而言之,关于Func的说法是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一段时间以来一直在使用 Func<> ,而且我设法避免了(暂时).但是,现在看来我无法永远躲避它.例如,我尝试了Dynamic Linq,但几乎所有功能都是以Func而言的.我已经尝试过我的一本书(C#2008/Deitel& Deitel)和MSDN,但是我还没试过.他们都直奔主题.

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. 关于Func可以说(用几句话)
  2. 我可以在网络上获得一些链接来开始解决这个问题吗?

感谢您的帮助

推荐答案

Func<> 是通用委托-使用起来非常方便,因为您不必创建自己的每个参数/返回类型组合的自己的委托.
之前,您必须编写类似以下内容的

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 );
    }
}

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

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 );
    }
}

其含义与第一个代码示例相同- Func< int,long> 定义了一个接受一个整数参数并返回一个long值的委托.

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.

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

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.

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

对于调用方,使用 MyDelegate Func<> 的解决方案之间没有区别.在这两种情况下,他都有三种选择来调用该方法:

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:

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

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

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

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

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;
}

这篇关于简而言之,关于Func的说法是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆