How would you go about using LINQ aggregate functions (ex. Sum, Average) on collections of bytes, shorts, and unsigned values? Granted, I’m a new C# programmer, but I can’t even figure out how to get something that compiles let alone has proper output.
Here’s a trivial example of what I am trying to do:
short[] numbersArray = { 5, 4, 1, 3, 9, 8, 6, 7, 2, 0 };
short sumArray = numbersArray.Sum();
or
List<short> numbersList = new List<short> { 5, 4, 1, 3, 9, 8, 6, 7, 2, 0 };
short sumList = numbersList.Sum();
I can’t either of these samples to work. If I change the data type to int it works, but I can’t get it to work for shorts, bytes, uints, etc.
What am I doing wrong?
Enumerable.Sum<T>()is only defined forIEnumerable<T>whereTinThis is because there is no addition operator* for
shortor any of the other primitive types (short + shortisint, for example).You have to say:
and less explicitly you can get away with
In this case, you are now invoking the overload:
*: Here I mean “operator” in the sense of an function
F:(short x short) -> short.