Sum Extension
I am sure those who know about Linq, must have used Sum extension method in their logic, in some way.
A basic example showing the use of Sum extension method.
Ex. Sum up the salary of employees in a generic list
void Main() | |
{ | |
List<Employee> lstEmployees = new List<Employee>{ | |
new Employee{ FirstName = "Mohan", LastName="Dave", Salary = 34000}, | |
new Employee{ FirstName = "Soham", LastName="Pal", Salary = 12300}, | |
new Employee{ FirstName = "Amit", LastName="Atare", Salary = 34478}, | |
new Employee{ FirstName = "Debashish", LastName="Das", Salary = 12500}, | |
new Employee{ FirstName = "Amit", LastName="Arora", Salary = 30000} | |
}; | |
var totalSalary = lstEmployees.Sum(item => item.Salary); | |
Console.WriteLine(totalSalary); | |
List<Product> lstProducts = new List<Product>{ | |
new Product{ Name = "Apple", Stock = 30}, | |
new Product{ Name = "Cheese", Stock = 40}, | |
new Product{ Name = "Orange", Stock = 23}, | |
new Product{ Name = "Pen", Stock = 79}, | |
new Product{ Name = "Pencil", Stock = 57} | |
}; | |
var totalStock = lstProducts.Sum(item => item.Stock); | |
Console.WriteLine(totalStock); | |
} | |
public class Employee | |
{ | |
public string FirstName { get; set; } | |
public string LastName { get; set; } | |
public double Salary { get; set; } | |
} | |
public class Product{ | |
public string Name {get; set; } | |
public int Stock {get; set; } | |
} |
And, why we wouldn't use it. It is powerful & simple to use.
But, have you ever wondered how to mimic the same functionality in your own extension method??
Sounds Interesting, Right !!
Let's get ready for the challenge !!
Let's see how....
I would be creating an extension method named SumEx. The signature would be same, so this version will be totally compatible with the previous example. We will see it in action.
This extension is a perfect example of how to make delegates into work, in a real world use case.
public static class SumExtensions { | |
public static int SumEx<T>(this IEnumerable<T> data, Func<T,int> callback) | |
{ | |
int output = 0; | |
var enumerator = data.GetEnumerator(); | |
while(enumerator.MoveNext()){ | |
var listItem = (T) enumerator.Current; | |
var value = callback(listItem); | |
output += value; | |
} | |
return output; | |
} | |
public static double SumEx<T>(this IEnumerable<T> data, Func<T, double> callback) | |
{ | |
double output = 0; | |
var enumerator = data.GetEnumerator(); | |
while (enumerator.MoveNext()) | |
{ | |
var listItem = (T)enumerator.Current; | |
var value = callback(listItem); | |
output += value; | |
} | |
return output; | |
} | |
// Repeat the function for rest of the data types like float, long, deicmal etc. | |
} |
Our extension method is ready now, let put it to test.
As you see the code is perfectly working fine, and results do tally up.
I hope this gives a good idea of how to write an extension method in a way the configuration can be passed from outside. Here, the configuration is which field you want to sum up i.e. Salary & Stock.
Happy Coding !!
Top comments (0)