Translating this sample into Visual Basic 9.0 produces almost identical code.
However, this translation is a bit naive because Visual Basic 9.0 actually provides syntax for more of the standard query operators than C# 3.0 does. While we have to call the "Sum" query operator explicitly in C#, Visual Basic allows us to use it directly in the query.
In fact, Visual Basic even allows us to create our own aggregate functions and use them directly in query expressions.
Here we get the product of the even numbers in the array. (I removed the expression to square each even number because it produced an OverflowException.)
I should point out that there is a behavioral difference that the Visual Basic "Aggregate" keyword introduces. A standard "From" query expression is delay evaluated. That is, the results aren't actually evaluated until they are accessed through, say, a "For Each" loop. However, an "Aggregate" query expression forces the results to be evaluated immediately. In contrast, C# 3.0 query expressions always produce results that are delay evaluated.1
1A bold statement that will be completely recanted if any reader can find an example that proves otherwise.2 2Please, prove me wrong. Seriously. I'm interested in this stuff.3 3This footnote motif is clearly ripped off from Raymond Chen.
Page rendered at Sunday, March 14, 2010 8:43:14 PM (Pacific Standard Time, UTC-08:00)
If you're interested in learning F#, this is the most comprehensive book available. The text is well written and the examples are instructive. And after all, the author is the inventor of F#.
Because this book provides source code in Standard ML, it's a fantastic resource for learning F#. One bit of warning: this book does not teach classic data structures. While structures such as binomial heaps and red-black trees are presented, it is assumed that the reader already knows and understands them.
Disclaimer The opinions expressed herein are my own personal opinions and do not represent my employer's view in any way.