Answer the question
In order to leave comments, you need to log in
Can you explain why in the this parameters of the function?
Can you explain why in the this parameters of the function?
Here is the code:
public static class MyExtensionMethods
{
public static decimal TotalPrices(this IEnumerable<Product> productEnum)
{
decimal total = 0;
foreach (Product prod in productEnum)
{
total += prod.Price;
}
return total;
}
}
Answer the question
In order to leave comments, you need to log in
Let there be an array Product[] products;
If you declare just a function (without this), then to call it you would have to write:
MyExtensionMethods.TotalPrices(products);
If you declare an extension method (with this) then you can write
products.TotalPrices();
Especially useful with LINQ
is decimal stuffPrice = products.Where(x => x.Price < 100).TotalPrices();
For details on google
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question