Skip to main content

There are several points that is possible to improve.

If you are working at least with c#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#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, 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 ingridientsingredients after you have created Formula.

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.

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.

formatting
Source Link
Akim
  • 475
  • 3
  • 13

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;

this.Ingredients = _Ingredients;

with: this.Ingredients = Ingredients.Select(ig => ig.Clone()).ToList() — so

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.

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.

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.

Source Link
Akim
  • 475
  • 3
  • 13

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.