tags:

views:

71

answers:

2

It seems that Sum is not defined for IEnumerable<uint> (and other unsigned integers, for that matter)

var s = new int[] { 1, 2, 3 };
s.Sum(); //works fine

var us = new uint[] { 1, 2, 3 };
us.Sum(); //missing method

I would like to know:

  • Have I done something fundamentally wrong/misunderstood the situation?
  • What design decisions might cause the omission of IEnumerable<uint>.Sum()?

MSDN: Enumerable.Sum

A: 

It might just be an oversight. I'm reminded of ForEach, which is available on Lists but not IEnumerable. I've written .ForEach as an extension method to IEnumerable in at least 3 projects.

mwilson
...Except that `ForEach` was not an oversight. It was deliberately not included in `IEnumerable` extension methods, since those are meant to be functional, and `ForEach` is fundamentally not functional. That said, it **should** be an extension method in IList; see [here](https://connect.microsoft.com/VisualStudio/feedback/details/552410/move-lots-of-helpful-methods-from-list-to-ilist-using-extension-methods) (and upvote!).
BlueRaja - Danny Pflughoeft
Done, thanks for the heads up!
mwilson
+5  A: 

Just a guess: Because uint is not CLS-compliant. Not sure if that would weigh in their decision to not support it.

Patrick Steele
Yep, that would do it. See [here](http://stackoverflow.com/questions/6325/why-are-unsigned-ints-not-cls-compliant)
BlueRaja - Danny Pflughoeft
+1 Good guess, but not definitive.
dss539