sum list of object property using recursions c#
class Program
{
static void Main(string[] args)
{
List<EMPLOYEE> employees = new List<EMPLOYEE>();
employees.Add(new EMPLOYEE { NAME = "AAAA", SALARY = 1 });
employees.Add(new EMPLOYEE { NAME = "BBBB", SALARY = 2 });
employees.Add(new EMPLOYEE { NAME = "CCCC", SALARY = 2 });
employees.Add(new EMPLOYEE { NAME = "DDDD", SALARY = 2 });
employees.Add(new EMPLOYEE { NAME = "EEEE", SALARY = 2 });
employees.Add(new EMPLOYEE { NAME = "FFFF", SALARY = 2 });
//Print total salary using recursion method in SumSalary
Console.WriteLine(SumSalary(employees, employees.Count));
Console.ReadKey();
}
public static decimal SumSalary(List<EMPLOYEE> ints, int total)
{
if (total <= 0)
return 0;
return SumSalary(ints, total - 1) + ints[total - 1].SALARY;
//return SumSalary(ints, total - 1) + ints.ElementAt(total - 1).SALARY;
}
public class EMPLOYEE
{
public string NAME { get; set; }
public decimal SALARY { get; set; }
}
}
{
static void Main(string[] args)
{
List<EMPLOYEE> employees = new List<EMPLOYEE>();
employees.Add(new EMPLOYEE { NAME = "AAAA", SALARY = 1 });
employees.Add(new EMPLOYEE { NAME = "BBBB", SALARY = 2 });
employees.Add(new EMPLOYEE { NAME = "CCCC", SALARY = 2 });
employees.Add(new EMPLOYEE { NAME = "DDDD", SALARY = 2 });
employees.Add(new EMPLOYEE { NAME = "EEEE", SALARY = 2 });
employees.Add(new EMPLOYEE { NAME = "FFFF", SALARY = 2 });
//Print total salary using recursion method in SumSalary
Console.WriteLine(SumSalary(employees, employees.Count));
Console.ReadKey();
}
public static decimal SumSalary(List<EMPLOYEE> ints, int total)
{
if (total <= 0)
return 0;
return SumSalary(ints, total - 1) + ints[total - 1].SALARY;
//return SumSalary(ints, total - 1) + ints.ElementAt(total - 1).SALARY;
}
public class EMPLOYEE
{
public string NAME { get; set; }
public decimal SALARY { get; set; }
}
}
Comments
Post a Comment