e.g. 5.18 Using a Delegate in an Extension Method
e.g. 5.19 Using the Filtering Extension Method with a Func
This commit is contained in:
parent
61cf1cbfaf
commit
be286cffbd
@ -21,7 +21,14 @@ namespace automatically_implemented_properties
|
||||
}
|
||||
};
|
||||
|
||||
foreach (Product prod in products.FilterByCategory("Soccer"))
|
||||
Func<Product, bool> categoryFilter = delegate(Product prod)
|
||||
{
|
||||
return prod.Category == "Soccer";
|
||||
};
|
||||
|
||||
IEnumerable<Product> filteredProducts = products.Filter(categoryFilter);
|
||||
|
||||
foreach (Product prod in filteredProducts)
|
||||
{
|
||||
Console.WriteLine("Name: {0}, Price {1:c}", prod.Name, prod.Price);
|
||||
}
|
||||
@ -55,5 +62,17 @@ namespace automatically_implemented_properties
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public static IEnumerable<Product> Filter(this IEnumerable<Product> productEnum,
|
||||
Func<Product, bool> selectorParam)
|
||||
{
|
||||
foreach (Product prod in productEnum)
|
||||
{
|
||||
if (selectorParam(prod))
|
||||
{
|
||||
yield return prod;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
Loading…
x
Reference in New Issue
Block a user