C# aint blunt!

written by tobinharris on June 26th, 2008 @ 06:36 PM

I'm finally getting time to dig into C# in Depth and try some stuff out. It's pleasing to see some of the terse constructs we now have.As a small example, this is great:

public decimal TotalPrice
{
     get
     {
         return 
            Lines.Sum(l => l.Price * l.Quantity * (IsVat ? 1.175m : 1) );
     }
}


kick it on DotNetKicks.com



Comments

  • Jon Skeet on 26 Jun 22:23

    If VAT is applied to the overall order rather than particular items, wouldn’t it make sense to move that logic outside the call to Sum?

    decimal withoutVat = Lines.Sum(l => l.Price * l.Quantity); return IsVat ? 1.175m * withoutVat : withoutVat;

    Of course, there could be some lines for which VAT is applied and some lines for which it isn’t (e.g. food) which changes things somewhat.

    Jon

  • Tobin Harris on 27 Jun 15:27

    Good point! And, if lines were individually vat sensitive, then perhaps this is better:

    Lines.Sum(l => l.Price * l.Quantity * (l.IsVat ? 1.175m : 1) );

Post a comment

Options:

Size

Colors