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-12 11:15:27 +10:00
|
|
|
|
// create and populate Shopping Cart, implementing IEnumerable<Product> interface
|
2021-07-12 12:07:21 +10:00
|
|
|
|
IEnumerable<Product> products = new ShoppingCart
|
2021-07-05 16:21:55 +10:00
|
|
|
|
{
|
2021-07-05 18:21:52 +10:00
|
|
|
|
Products = new List<Product>
|
|
|
|
|
{
|
2021-07-12 12:15:56 +10:00
|
|
|
|
new Product {Name = "Kayak", Category = "Watersports", Price = 275M},
|
|
|
|
|
new Product {Name = "Lifejacket", Category = "Watersports", Price = 48.95M},
|
|
|
|
|
new Product {Name = "Soccer ball", Category = "Soccer", Price = 19.50M},
|
|
|
|
|
new Product {Name = "Corner flag", Category = "Soccer", Price = 34.95M}
|
2021-07-05 18:21:52 +10:00
|
|
|
|
}
|
|
|
|
|
};
|
2021-07-12 13:28:54 +10:00
|
|
|
|
|
|
|
|
|
Func<Product, bool> categoryFilter = delegate(Product prod)
|
|
|
|
|
{
|
|
|
|
|
return prod.Category == "Soccer";
|
|
|
|
|
};
|
2021-07-12 12:15:56 +10:00
|
|
|
|
|
2021-07-12 13:28:54 +10:00
|
|
|
|
IEnumerable<Product> filteredProducts = products.Filter(categoryFilter);
|
|
|
|
|
|
|
|
|
|
foreach (Product prod in filteredProducts)
|
2021-07-12 11:15:27 +10:00
|
|
|
|
{
|
2021-07-12 12:15:56 +10:00
|
|
|
|
Console.WriteLine("Name: {0}, Price {1:c}", prod.Name, prod.Price);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
decimal total = products.FilterByCategory("Soccer").TotalPrices();
|
|
|
|
|
Console.WriteLine("Filtered total: {0:c}", total);
|
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-10 16:36:55 +10:00
|
|
|
|
public static decimal TotalPrices(this IEnumerable<Product> productEnum)
|
2021-07-05 15:05:57 +10:00
|
|
|
|
{
|
2021-07-05 18:21:52 +10:00
|
|
|
|
decimal total = 0;
|
2021-07-10 16:36:55 +10:00
|
|
|
|
foreach (Product prod in productEnum)
|
2021-07-05 18:21:52 +10:00
|
|
|
|
{
|
|
|
|
|
total += prod.Price;
|
|
|
|
|
}
|
|
|
|
|
return total;
|
2021-07-05 15:05:57 +10:00
|
|
|
|
}
|
2021-07-12 12:15:56 +10:00
|
|
|
|
|
|
|
|
|
public static IEnumerable<Product> FilterByCategory(this IEnumerable<Product> productEnum,
|
|
|
|
|
string categoryParam)
|
|
|
|
|
{
|
|
|
|
|
foreach (Product prod in productEnum)
|
|
|
|
|
{
|
|
|
|
|
if (prod.Category == categoryParam)
|
|
|
|
|
{
|
|
|
|
|
yield return prod;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
2021-07-12 13:28:54 +10:00
|
|
|
|
|
|
|
|
|
public static IEnumerable<Product> Filter(this IEnumerable<Product> productEnum,
|
|
|
|
|
Func<Product, bool> selectorParam)
|
|
|
|
|
{
|
|
|
|
|
foreach (Product prod in productEnum)
|
|
|
|
|
{
|
|
|
|
|
if (selectorParam(prod))
|
|
|
|
|
{
|
|
|
|
|
yield return prod;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
2021-07-05 15:05:57 +10:00
|
|
|
|
}
|
|
|
|
|
}
|