Wednesday, September 2, 2015

When to make a giant foreach loop in C#.

When calculating totals or counts as suggested in the last method here, you will be looping through the collection once for each total so if you have six column footers wherein you add up six prices across all line items (maybe total of subtotals, total of taxes, total of subtotals combined with tax fees which are real totals, and a variant of these three for when a discount code is used), well you've just looped through your collection six times instead of just once. You might be ahead to loop through once elsewhere in code, cast the findings to private fields, and then pluck the data points out again for the ASPxGridView footer assignments. My post "James and the Giant foreach" suggests this is out of fashion anymore, but I think unnecessary performance overhead is a bigger sin.

No comments:

Post a Comment