Skip to main content

Practice · 3 of 3

Sum any numeric type

CHALLENGE
Difficulty: advanced+25 XP

Implement static T Sum<T>(T[] values) where T : INumber<T> summing all elements starting from T.Zero, and static T Average<T>(T[] values) where T : INumber<T> returning Sum / count (use T.CreateTruncating for the count). Works for int, double, decimal — one implementation.

Back to lesson: Practice: Generic machinery