4 ms·
C# arrays don't. However, C# arrays are IEnumerable<T>, which does.
by neonsunset 2y ago
C# arrays don't. However, C# arrays are IEnumerable<T>, which does.
- lupire 2y agoIEnunerable<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.enumerable.sum?view=net-8.0 https://learn.microsoft.com/en-us/dotnet/api/system.linq.enu...
- deleted 2y ago[deleted]
- masfuerte 2y agoSo you can't use IEnumerable<T>.Sum if T is a custom type implementing operator+ ?
- deleted 2y ago[deleted]
- neonsunset 2y agoYou 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/math https://learn.microsoft.com/en-us/dotnet/standard/generics/m... [1]: https://github.com/dotnet/runtime/issues/64031 https://github.com/dotnet/runtime/issues/64031