i have the following methods:
public int CountProperty1
{
get
{
int count = 0;
foreach (var row in Data)
{
count = count + row.Property1;
}
return count ;
}
}
public int CountProperty2
{
get
{
int count = 0;
foreach (var row in Data)
{
count = count + row.Property2;
}
return count ;
}
}
what is the best way to avoid duplication here and share as much code as possible
How about using LINQ and the Sum extension method ?
In case that is not an option, you can refactor out the logic into your own sum method:
Note about the last example: I made up the “Row” type, as it was not evident from your example. Substitute with the proper type.