有人可以告訴我使用委託的好處,而不是像下面所示的那樣調用函數本身(或換句話說,爲什麼選擇選項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