/
Iterators.cs
46 lines (34 loc) · 926 Bytes
/
Iterators.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
namespace TheForEachMethodAndForeachStatement;
public static class Iterators
{
public static int GetTotalOfIntListWithForEachMethod(List<int> prices)
{
int total = 0;
prices.ForEach(x => total += x);
return total;
}
public static int GetTotalOfIntListWithForeachStatement(List<int> prices)
{
int total = 0;
foreach (var price in prices)
{
total += price;
}
return total;
}
public static int GetTotalOfProductsListWithForEachMethod(List<Product> products)
{
int total = 0;
products.ForEach(x => total += x.Price);
return total;
}
public static int GetTotalOfProductsListWithForeachStatement(List<Product> products)
{
int total = 0;
foreach (var product in products)
{
total += product.Price;
}
return total;
}
}