e.g. 5.12 Implement and Interface in ShoppingCart

e.g. 5.13 An Extension Method That works on an Interface
This commit is contained in:
Jason Zhu 2021-07-10 16:36:55 +10:00
parent aa49052a81
commit f98ec2cbc7
2 changed files with 15 additions and 4 deletions

View File

@ -30,10 +30,10 @@ namespace automatically_implemented_properties
public static class MyExtensionMethod public static class MyExtensionMethod
{ {
public static decimal TotalPrices(this ShoppingCart cartParam) public static decimal TotalPrices(this IEnumerable<Product> productEnum)
{ {
decimal total = 0; decimal total = 0;
foreach (Product prod in cartParam.Products) foreach (Product prod in productEnum)
{ {
total += prod.Price; total += prod.Price;
} }

View File

@ -1,9 +1,20 @@
using System.Collections.Generic; using System.Collections;
using System.Collections.Generic;
namespace automatically_implemented_properties namespace automatically_implemented_properties
{ {
public class ShoppingCart public class ShoppingCart : IEnumerable<Product>
{ {
public List<Product> Products { get; set; } public List<Product> Products { get; set; }
public IEnumerator<Product> GetEnumerator()
{
return Products.GetEnumerator();
}
IEnumerator IEnumerable.GetEnumerator()
{
return GetEnumerator();
}
} }
} }