Question

Hello friends,

    public decimal Balance
    {
        get 
        {
            _test = 0m;
            foreach (ITransaction t in _Transactions)
            {
                _test += t.Value;
            }
            return _OpeningBalance + _test;
        }
    }

The Value variable of ITransaction and the _OpeningBalance instance variable are both decimals. Is there a shorter way to return the sum of the ITransaction values and the _OpeningBalance without using a 'temporary variable'? This is for a university assignment and I want the code to be as neat as possible. Thanks in advance :)

Was it helpful?

Solution

Try LINQ Sum()

return _OpeningBalance + _Transactions.Sum(t => t.Value);

You can find an example along with documentation on MSDN.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top