Yesterday I responded to Paul Stovell’s post about Ruby for .NET developers, in that post I showed how you could use the LINQ expression with the List<T> object’s ForEach method to match what Paul had done with Ruby block syntax. While it still may not be able to write code quite like Scott Hanselman demonstrates I think that it goes a long way towards showing that C# isn’t a programming language with trainer-wheels.
One of the things that I wasn’t sure of yesterday was whether you could have an expression that returned a void value. Well, as I was reading my feeds this morning I stumbled across this post by Ido Samuelson titled Control.Invoke in C# 3.0.
Ido’s post is interesting because he combines extension methods, LINQ expressions and even method group conversion syntax (in an interim example) to show how you can more easily approach the challenges of calling back onto the message handling loop inside a windows application. I highly recommend looking at his post because I think it shows one of the useful areas where extension methods will be used outside of an application’s data path.
So – yes you can have void expressions, and as a matter of fact you can have void(void) expressions which means expression syntax is just a more concise version of anonymous method syntax.
June 23, 2007 at 7:14 pm
Here’s a slightly more compact version
private void UpdateProgress(int percentComplete) {
this.SafeInvoke(() => {
_progressBar.Value = percentComplete;
});
}
Read more here: http://keithhill.spaces.live.com/blog/cns!5A8D2641E0963A97!765.entry