Compare commits

..

No commits in common. "chap5" and "master" have entirely different histories.

2 changed files with 16 additions and 63 deletions

View File

@ -1,7 +1,6 @@
using System; using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.Diagnostics; using System.Diagnostics;
using System.Linq;
using automatically_implemented_properties; using automatically_implemented_properties;
namespace automatically_implemented_properties namespace automatically_implemented_properties
@ -10,70 +9,35 @@ namespace automatically_implemented_properties
{ {
static void Main(string[] args) static void Main(string[] args)
{ {
Product[] products = // create and populate Shopping Cart
ShoppingCart cart = new ShoppingCart
{ {
new Product {Name = "Kayak", Category = "Watersports", Price = 275M}, Products = new List<Product>
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}
};
var results = from product in products
orderby product.Price descending
select new
{ {
product.Name, new Product {Name = "Kayak", Price = 275M},
product.Price new Product {Name = "Lifejacket", Price = 48.95M},
}; new Product {Name = "Soccer ball", Price = 19.50M},
new Product {Name = "Corner flag", Price = 34.95M}
int count = 0;
// print out the names
foreach (var p in results)
{
Console.WriteLine("Item: {0}, Cost: {1}", p.Name, p.Price);
if (++count == 3)
{
break;
}
} }
};
// get total value of the products in cart
decimal cartTotal = cart.TotalPrices();
Console.WriteLine("Total: {0:c}", cartTotal);
} }
} }
public static class MyExtensionMethod public static class MyExtensionMethod
{ {
public static decimal TotalPrices(this IEnumerable<Product> productEnum) public static decimal TotalPrices(this ShoppingCart cartParam)
{ {
decimal total = 0; decimal total = 0;
foreach (Product prod in productEnum) foreach (Product prod in cartParam.Products)
{ {
total += prod.Price; total += prod.Price;
} }
return total; return total;
} }
public static IEnumerable<Product> FilterByCategory(this IEnumerable<Product> productEnum,
string categoryParam)
{
foreach (Product prod in productEnum)
{
if (prod.Category == categoryParam)
{
yield return prod;
}
}
}
public static IEnumerable<Product> Filter(this IEnumerable<Product> productEnum,
Func<Product, bool> selectorParam)
{
foreach (Product prod in productEnum)
{
if (selectorParam(prod))
{
yield return prod;
}
}
}
} }
} }

View File

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