Func代表vs功能 [英] Func Delegate vs Function

查看:152
本文介绍了Func代表vs功能的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有人可以告诉我使用代理的优点,而不是如下所示调用函数本身(或者换句话说,为什么选择选项A超过选项B)?昨天晚上我正在看一个人的linq代码,他们有一个类似于Option A的东西,但它被用来返回一个编译的linq查询。



我意识到前者现在可以被传递到其他功能..只是不确定其实用性。 BTW,我意识到这将不会按原样编译..在发布之前未注释的功能之一。 TYIA

  class Program 
{
static void Main(string [] args)
{
Console.WriteLine(SayTwoWords(Hello,World));
Console.ReadKey();
}

//选项A
private static Func< string,string,string>
SayTwoWords =(a,b)=> String.Format({0} {1},a,b);

//选项B
私有静态字符串SayTwoWords(string a,string b)
{
return String.Format({0} {1}, a,b);
}
}

*********** *编辑************



不知道如果它更好地解释了我的问题,但这里是一个原始类型的代码的例子我想到这一点:

  public static class clsCompiledQuery 
{
public static Func< DataContext,string, IQueryable< clsCustomerEntity>>
getCustomers = CompiledQuery.Compile((DataContext db,string strCustCode)
=> from objCustomer in db.GetTable< clsCustomerEntity>()
其中objCustomer.CustomerCode == strCustCode
选择objCustomer);
}

以这种方式编写函数有什么好处吗?

解决方案

您发布的代码没有任何优势。在您的代码中,使用代理只会增加复杂性以及额外的运行时费用,所以您最好直接调用方法。



但是,代理有很多用途。 传递到其他方法是主要的用法,虽然存储函数和使用它以后也是非常有用的。



LINQ完全基于这个概念。当你这样做:

  var results = myCollection.Where(item => item ==Foo); 

您正在传递一个委托(定义为lambda: item => ; item ==Foo)到LINQ库中的其中函数。这是什么使它正常工作。


Can someone tell me the advantages of using a delegate as opposed to calling the function itself as shown below (or in other words why choose Option A over Option B)? I was looking at someone's linq code last night and they had something similar to Option A but it was being used to return a compiled linq query.

I realize the former can now be passed around to other functions.. just not sure of its practicality. BTW, I realize this wouldn't compile as-is.. uncommented one of the functions before posting. TYIA

class Program
{
    static void Main(string[] args)
    {   
        Console.WriteLine(SayTwoWords("Hello", "World"));
        Console.ReadKey();
    }

    // Option A
    private static Func<string, string, string>
        SayTwoWords = (a, b) => String.Format("{0} {1}", a, b);

    // Option B
    private static string SayTwoWords(string a, string b)
    {
        return String.Format("{0} {1}", a, b);
    }        
}

************EDIT************

Not sure if it explains my question better but here is an example of the type of code that originally got me thinking about this:

public static class clsCompiledQuery
{
    public static Func<DataContext, string, IQueryable<clsCustomerEntity>>
        getCustomers = CompiledQuery.Compile((DataContext db, string strCustCode)
            => from objCustomer in db.GetTable<clsCustomerEntity>()
            where objCustomer.CustomerCode == strCustCode
            select objCustomer);
}

Is there any advantage to writing a function in this way?

解决方案

There is no advantage in the code you posted. In your code, using the delegate just adds complexity as well as an extra runtime cost - so you're better off just calling the method directly.

However, delegates have many uses. "Passing around" to other methods is the primary usage, though storing a function and using it later is also very useful.

LINQ is built on top of this concept entirely. When you do:

var results = myCollection.Where(item => item == "Foo");

You're passing a delegate (defined as a lambda: item => item == "Foo") to the Where function in the LINQ libraries. This is what makes it work properly.

这篇关于Func代表vs功能的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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