NullableSum

        public static double? NullableSum(this IEnumerable<double?> source)
        {
            double? sum = null;
            foreach (var v in source)
            {
                if (v == null) continue;
                if (sum == null)
                {
                    sum = 0;
                }

                sum += v.GetValueOrDefault();
            }

            return sum;
        }
Example:
            var myNumbers = new List<double?>
            {
                null,1,2
            };

            Assert.AreEqual(3, myNumbers.NullableSum());

Description

Takes an array of nullable values and sums them up. Can be easily replaced with int?

Details

  • Author: kl00t
  • Submitted on: 17-6-2019 13:18:49
  • Language: C#
  • Type: double?
  • Views: 155

Double click on the code to select all.

 

;