Re:Clever code is probably the worst code you could write
by masfuerte on Tuesday May 21, 2024 @07:35PM
(#40435564)
So you can't use IEnumerable<T>.Sum if T is a custom type implementing operator+ ?
Re:Clever code is probably the worst code you could write
by neonsunset on Tuesday May 21, 2024 @07:49PM
(#40435690)
You could, but .Sum is a little more specific than that - it is non-overflowing sum.
Historically, constraining generic arguments on addition was problematic - the full feature set of numeric types was "lifted" to be fully representable through generics only recently[0].
With that said, there is an open proposal[1] to introduce additional generic math overloads to IEnumerable<T> methods, but it hasn't seen much activity as the existing overloads cover most commonly used numeric types already.
[0]: https://learn.microsoft.com/en-us/dotnet/standard/generics/m...
Only collections of numeric types (including arrays) have Sum.
https://learn.microsoft.com/en-us/dotnet/api/system.linq.enu...