There are several points that is possible to improve.
If you are working at least with C# 2.0 (as I expect because you mentioned Nullable<decimal>
), you could replace ArrayList
with List<Ingredient>
or even IEnumerable<Ingredient>
. This will make it more intuitive, and later you could remove type casting from SumPercentages
.
foreach (Ingredient ig in f.Ingredients)
{
SumOfPercentages += ig.Percentage;
}
Then, if you are working at least with C# 3.0 you could, rewrite SumPercentages
using LINQ:
public void SumPercentages(Formula f)
{
f.SumPercentages = f.Ingredients.Select(ig => ig.Percentage).Sum();
}
Last point, to implement ICloneable
for both Formula
and Ingredient
, then replace
this.Ingredients = _Ingredients;
with
this.Ingredients = Ingredients.Select(ig => ig.Clone()).ToList();
So Ingredients
will be a copy of the initial array. This will prevent a situation when you unwittingly change the list of ingredients after you have created Formula
.