// 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); }
In the case above, option B is what I would go with if I don't need the SayTwoWords functionality that needs to be changed. With option A, a different function may be assigned to SayTwoWords . Catch the more detailed differences in this answer :
There is a situation where option A makes sense. Consider the case where you must compile an expression for a delegate. Since compiling an expression is difficult, this is what you would like to do it only once. A similar pattern helps:
public static class Cache<T> { public static readonly Func<T> Get = GetImpl(); static Func<T> GetImpl() {
instead
public static class Cache<T> { public static T Get() {
In the first case, when you call Get , GetImpl is executed only once, where, as in the second case, the (expensive) Get will be called each time.
nawfal Jun 29 '13 at 13:55 on 2013-06-29 13:55
source share