5.1.4 Applying Extension Methods to an Interface

This commit is contained in:
Jason Zhu 2021-07-12 11:15:27 +10:00
parent f98ec2cbc7
commit 3b938d8c61

View File

@ -9,8 +9,8 @@ namespace automatically_implemented_properties
{ {
static void Main(string[] args) static void Main(string[] args)
{ {
// create and populate Shopping Cart // create and populate Shopping Cart, implementing IEnumerable<Product> interface
ShoppingCart cart = new ShoppingCart ShoppingCart products = new ShoppingCart
{ {
Products = new List<Product> Products = new List<Product>
{ {
@ -20,10 +20,22 @@ namespace automatically_implemented_properties
new Product {Name = "Corner flag", Price = 34.95M} new Product {Name = "Corner flag", Price = 34.95M}
} }
}; };
// create and populate an array of Product objects, implementing IEnumerable<Product> interface
Product[] productArray =
{
new Product {Name = "Kayak", Price = 275M},
new Product {Name = "Lifejacket", Price = 48.95M},
new Product {Name = "Soccer ball", Price = 19.50M},
new Product {Name = "Corner flag", Price = 34.95M}
};
// get total value of the products in cart // get total value of the products in cart
decimal cartTotal = cart.TotalPrices(); decimal cartTotal = products.TotalPrices();
decimal arrayTotal = productArray.TotalPrices();
Console.WriteLine("Total: {0:c}", cartTotal); Console.WriteLine("Total: {0:c}", cartTotal);
Console.WriteLine("Array Total: {0:c}", arrayTotal);
} }
} }