Today I saw some C# language syntax that made me think, "hey, wait a minute, I didn’t know you could do that!", so perhaps it’ll be new to someone else too. Honestly, I don’t know how I missed this memo.
Let’s start off with the code, then we’ll talk about it. I’ll including some additional lambda stuff here for the fun of it. Sample code project: DelegateTypeInference.zip
Look at line #11, okay, that’s been in there since C# day 1, the ability to pass a method (TinyCall) as a parameter (to function A) with a strongly typed method signature (ThreadStart). Then in C# 2.0 one could use line #12 with the keyword delegate to create an anonymous function (no method name here, just some code).
What I didn’t know until today is that line #13 would work, that you can just pass the method name as the parameter and the delegate type would be inferred. Nice and easy. Line #14/#15 was also new to me, well at least the part about using () for an empty parameter list.
Thrown in are some more C# 3.0 lambda expressions just for the heck of it. Line #14 is an anonymous function with no return type (void), no parameters, and only one call within the function body (like line #12). Line #15 has more than one call with the same structure. Lines #17 & #18 are included for reference of other common lambda uses using different method signatures.