There are several points that 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 copy of initial array. This will prevent situation when you unwittingly change list of ingridients after you have created Formula
.