I am trying to convert the following code for the variance calculation
public static double Variance(this IEnumerable<double> source)
{
double avg = source.Average();
double d = source.Aggregate(0.0,
(total, next) => total += Math.Pow(next - avg, 2));
return d / (source.Count() - 1);
}
described on CodeProject into corresponded VB.NET lambda expression syntax, but I am stuck in the conversion of Aggregate function.
How can I implement that code in VB.NET?
The following will only work in VB 10. Prior versions didn’t support multi-line lambdas.
Function(foo) barcorresponds to the single-statement lambda(foo) => barin C# but you need the multi-line lambda here which only exists since VB 10.However, I’m wary of the original code. Modifying
totalseems like an error, since noAggregateoverload passes its arguments by reference. So I’m suggesting that the original code is wrong (even though it may actually compile), and that the correct solution (in VB) would look like this:Furthermore, this doesn’t require any multi-line lambdas, and thus also works on older versions of VB.