I have a simple method like this:
private void CalculateTotals(IEnumerable<MyData> data)
{
decimal value1 = data.Sum(d => d.Value1);
decimal value2 = data.Sum(d => d.Value2);
}
This works and allows for any type of collection to be passed in, but this will as well perform multiple enumeration of data that can be quite costly as well.
Now I can change the type of parameter to ICollection<MyData>
but that will mean that the caller does not know if the method can/will change the collection since it's possible to call Add/Remove
on it.
My next guess is IReadonlyCollection<MyData>
that seems most fitting, but that will mean every caller will have to create/transform to a new ReadonlyCollection
.
Are there any "preferred" ways of solving this in .Net?