public class Foo
{
public ushort Weight { get; }
}
public class Bar<T> : IEnumerable where T : Foo
{
private Collection<T> _contents;
...
public ulong TotalWeight { get { return _contents.Sum(a => a.Weight); } }
}
I’m expecting the total to add up to more than the maximum value of a ushort.
I’m getting an Intellisense error: “Ambiguous Invocation”, with a list of numeric types that doesn’t include ushort or ulong. I’m not sure what it wants.
I also tried using Select (from this post), as follows:
_contents.Select(a => a.Weight).Sum()
but Intellisense complains that it cannot resolve the Sum method and lists a bunch of candidates that also does not include ushort or ulong. Again, I’m not sure what it wants.
I apologize if this is really newbish, I just don’t understand what Intellisense is telling me.
There’s no
Sumoverload which applies to aushortsequence or to a general sequence and a projection toushort. The simplest approach would be to simply cast tolong:This will alleviate the overflow problem as well. Note that there is no overload using
ulongeither. If you have enoughushortvalues to overflowlong, it’s probably going to take a while to add them up anyway 🙂