M
M
Max Suprunenko2015-11-04 20:22:05
C++ / C#
Max Suprunenko, 2015-11-04 20:22:05

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

1 answer(s)
C
cjey, 2015-11-04
@msuprunenko

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 question

Ask a Question

731 491 924 answers to any question