News for hackers. Stuff that matters.
Re:Clever code is probably the worst code you could write by neonsunset on Tuesday May 21, 2024 @07:22PM (#40435436)
C# arrays don't. However, C# arrays are IEnumerable<T>, which does.
Re:Clever code is probably the worst code you could write by lupire on Tuesday May 21, 2024 @07:32PM (#40435535)
IEnunerable<T> of course does not have Sum.

Only collections of numeric types (including arrays) have Sum.

https://learn.microsoft.com/en-us/dotnet/api/system.linq.enu...

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...

[1]: https://github.com/dotnet/runtime/issues/64031