有人可以告诉我使用委托的好处,而不是像下面所示的那样调用函数本身(或换句话说,为什么选择选项A而不是选项B)?昨天晚上我正在查看某人的linq代码,他们有类似于选项A的东西,但它被用来返回一个已编译的linq查询。Func委托与功能
我意识到前者现在可以传递给其他功能..只是不确定其实用性。顺便说一句,我意识到这不会按原样编译..在发布之前取消注释了其中一项功能。 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);
}
}
************编辑************
不知道是否说明我的问题更好,但这里有一个例子原来让我想到这种类型的代码类型:
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);
}
以这种方式编写函数是否有优势?
可能重复的[我仍然没有得到委托](http://stackoverflow.com/questions/399222/i-still-dont-get-delegates) – 2010-06-24 19:25:30