Compare commits

...

10 Commits

Author SHA1 Message Date
Jason Zhu bf4a437acd e.g. 5.27 Using LINQ to Query Data 2021-07-15 17:00:53 +10:00
Jason Zhu e2aa8c7cba e.g. 5.26 Querying Without LINQ 2021-07-15 16:56:14 +10:00
Jason Zhu a376175c76 5.1.6 Using Lambda Expression
e.g. 5.22 Extending the Filtering Expression by Lambda
2021-07-12 13:36:35 +10:00
Jason Zhu 0e57f0e2e8 e.g. 5.20 A Lambda Expression Without a Func 2021-07-12 13:33:48 +10:00
Jason Zhu c95b347b15 e.g. 5.20 Using a Lambda Expression to Replace a Delegate Definition 2021-07-12 13:32:22 +10:00
Jason Zhu be286cffbd e.g. 5.18 Using a Delegate in an Extension Method
e.g. 5.19 Using the Filtering Extension Method with a Func
2021-07-12 13:28:54 +10:00
Jason Zhu 61cf1cbfaf 5.1.5 Creating Filtering Extension Methods 2021-07-12 12:15:56 +10:00
Jason Zhu ec983ff21c 5.1.4 Applying Extension Methods to an Interface (Bug fix) 2021-07-12 12:07:21 +10:00
Jason Zhu 3b938d8c61 5.1.4 Applying Extension Methods to an Interface 2021-07-12 11:15:27 +10:00
Jason Zhu f98ec2cbc7 e.g. 5.12 Implement and Interface in ShoppingCart
e.g. 5.13 An Extension Method That works on an Interface
2021-07-12 10:25:34 +10:00
2 changed files with 64 additions and 17 deletions

View File

@ -1,6 +1,7 @@
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using automatically_implemented_properties;
namespace automatically_implemented_properties
@ -9,35 +10,70 @@ namespace automatically_implemented_properties
{
static void Main(string[] args)
{
// create and populate Shopping Cart
ShoppingCart cart = new ShoppingCart
Product[] products =
{
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}
}
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}
};
// get total value of the products in cart
decimal cartTotal = cart.TotalPrices();
Console.WriteLine("Total: {0:c}", cartTotal);
var results = from product in products
orderby product.Price descending
select new
{
product.Name,
product.Price
};
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;
}
}
}
}
public static class MyExtensionMethod
{
public static decimal TotalPrices(this ShoppingCart cartParam)
public static decimal TotalPrices(this IEnumerable<Product> productEnum)
{
decimal total = 0;
foreach (Product prod in cartParam.Products)
foreach (Product prod in productEnum)
{
total += prod.Price;
}
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,9 +1,20 @@
using System.Collections.Generic;
using System.Collections;
using System.Collections.Generic;
namespace automatically_implemented_properties
{
public class ShoppingCart
public class ShoppingCart : IEnumerable<Product>
{
public List<Product> Products { get; set; }
public IEnumerator<Product> GetEnumerator()
{
return Products.GetEnumerator();
}
IEnumerator IEnumerable.GetEnumerator()
{
return GetEnumerator();
}
}
}