I have Following list of products
List<Product> products = new List<Product> {
new Product {
ProductID = 1,
ProductName = "first candy",
UnitPrice = (decimal)10.0 },
new Product {
ProductID = 2,
ProductName = "second candy",
UnitPrice = (decimal)35.0 },
new Product {
ProductID = 3,
ProductName = "first vegetable",
UnitPrice = (decimal)6.0 },
new Product {
ProductID = 4,
ProductName = "second vegetable",
UnitPrice = (decimal)15.0 },
new Product {
ProductID = 5,
ProductName = "third product",
UnitPrice = (decimal)55.0 }
};
var veges1 = products.Get(IsVege); //Get is a Extension method and IsVege is a Predicate
//Predicate
public static bool IsVege(Product p)
{
return p.ProductName.Contains("vegetable");
}
//Extension Method
public static IEnumerable<T> Get<T>(this IEnumerable<T> source, Func<T, bool> predicate)
{
foreach (T item in source)
{
if (predicate(item))
yield return item;
}
}
I know am very late on these topics but still am trying to understand by debugging in Visual Studio
i have break point in all the functions(Predicate,Extension Method)
My question is
1.What happens when the below line gets executed
var veges1 = products.Get(IsVege); // i dont see the breakpoint hitting either predicate or GET method)
But in my results view while debugging i see output for veges1.
If i hit the below code
veges1.Count() // Breakpoint in Predicate and GET is hit and i got the count value.
How does this work ? Can you give some understanding.
PS: i know there are lot of examples and questions on this. Am trying to understand with this example as it ll make me easier to get things.
Updated Question
The same sample i did above am trying to do the same with Lamda Expression
var veges4 = products.Get(p => p.ProductName.Contains("vegetable"));
and i get results as expected.
where GET is my Extension method but when that line is executed my break points on GET method was never called?
Thanks