2021-07-05 15:05:57 +10:00
|
|
|
|
using System;
|
2021-07-05 18:21:52 +10:00
|
|
|
|
using System.Collections.Generic;
|
2021-07-05 15:54:28 +10:00
|
|
|
|
using System.Diagnostics;
|
2021-07-05 18:21:52 +10:00
|
|
|
|
using automatically_implemented_properties;
|
2021-07-05 15:05:57 +10:00
|
|
|
|
|
|
|
|
|
namespace automatically_implemented_properties
|
|
|
|
|
{
|
|
|
|
|
class Program
|
|
|
|
|
{
|
|
|
|
|
static void Main(string[] args)
|
|
|
|
|
{
|
2021-07-05 18:21:52 +10:00
|
|
|
|
// create and populate Shopping Cart
|
|
|
|
|
ShoppingCart cart = new ShoppingCart
|
2021-07-05 16:21:55 +10:00
|
|
|
|
{
|
2021-07-05 18:21:52 +10:00
|
|
|
|
Products = new List<Product>
|
|
|
|
|
{
|
|
|
|
|
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
|
|
|
|
|
decimal cartTotal = cart.TotalPrices();
|
|
|
|
|
|
|
|
|
|
Console.WriteLine("Total: {0:c}", cartTotal);
|
2021-07-05 15:54:28 +10:00
|
|
|
|
}
|
|
|
|
|
|
2021-07-05 15:05:57 +10:00
|
|
|
|
}
|
|
|
|
|
|
2021-07-05 18:21:52 +10:00
|
|
|
|
public static class MyExtensionMethod
|
2021-07-05 15:05:57 +10:00
|
|
|
|
{
|
2021-07-05 18:21:52 +10:00
|
|
|
|
public static decimal TotalPrices(this ShoppingCart cartParam)
|
2021-07-05 15:05:57 +10:00
|
|
|
|
{
|
2021-07-05 18:21:52 +10:00
|
|
|
|
decimal total = 0;
|
|
|
|
|
foreach (Product prod in cartParam.Products)
|
|
|
|
|
{
|
|
|
|
|
total += prod.Price;
|
|
|
|
|
}
|
|
|
|
|
return total;
|
2021-07-05 15:05:57 +10:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|