How do I describe an Action delegate that returns a value (non-void)?

Yes. Func<> returns the type specified as the final generic type parameter, such that Func<int> returns an int and Func<int, string> accepts an integer and returns a string. Examples: Func<int> getOne = () => 1; Func<int, string> convertIntToString = i => i.ToString(); Action<string> printToScreen = s => Console.WriteLine(s); // use them printToScreen(convertIntToString(getOne()));

Non-retaining array for delegates

I found this bit of code awhile ago (can’t remember who to attribute it to). It’s quite ingenius, using a Category to allow the creation of a mutable array that does no retain/release by backing it with a CFArray with proper callbacks. @implementation NSMutableArray (WeakReferences) + (id)mutableArrayUsingWeakReferences { return [self mutableArrayUsingWeakReferencesWithCapacity:0]; } + (id)mutableArrayUsingWeakReferencesWithCapacity:(NSUInteger)capacity { … Read more

What is the lifetime of a delegate created by a lambda in C#?

It’s not guaranteed either way. From what I remember of the current MS implementation: A lambda expression which doesn’t capture any variables is cached statically A lambda expression which only captures “this” could be captured on a per-instance basis, but isn’t A lambda expression which captures a local variable can’t be cached Two lambda expressions … Read more

Delegate vs. delegate keyword

From http://msdn.microsoft.com/en-us/library/system.delegate.aspx: The Delegate class is the base class for delegate types. However, only the system and compilers can derive explicitly from the Delegate class or from the MulticastDelegate class. It is also not permissible to derive a new type from a delegate type. The Delegate class is not considered a delegate type; it is … Read more

How to create a delegate from a MethodInfo when method signature cannot be known beforehand?

You can use System.Linq.Expressions.Expression.GetDelegateType method: using System; using System.Linq; using System.Linq.Expressions; using System.Reflection; class Program { static void Main() { var writeLine = CreateDelegate(typeof(Console).GetMethod(“WriteLine”, new[] { typeof(string) })); writeLine.DynamicInvoke(“Hello world”); var readLine = CreateDelegate(typeof(Console).GetMethod(“ReadLine”, Type.EmptyTypes)); writeLine.DynamicInvoke(readLine.DynamicInvoke()); } static Delegate CreateDelegate(MethodInfo method) { if (method == null) { throw new ArgumentNullException(“method”); } if (!method.IsStatic) { throw … Read more

Is using Action.Invoke considered best practice?

The two are equivalent, the compiler converts OnAdd(“It Happened”); into OnAdd.Invoke(“It Happened”); for you. I guess it’s a matter of preference, however I personally prefer the terser form. As an aside, it is generally preferable to take a local copy of a class level delegate before invoking it to avoid a race condition whereby OnAdd … Read more

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)