# How can calculate the sum in a list with anonymous methods?

• ### Question

• Hi,

I have a small program that needs to calculate the sum of all numbers in a list with an anonymous method.I have the following delegates:

``` public delegate void filterElements(int x);
public delegate bool IntPr(int x);
public delegate void IntAc(int x);
public class IntList : List<int>
{
public IntList(params int[] elements) : base(elements)
{

}
public void Act(IntAc f)
{
foreach (int i in this)
{
f(i);
}
}
public IntList Filter(IntPr p)
{
IntList res = new IntList();
foreach (int i in this)
{
if (p(i))

}
return res;
}
public void SumNumbers(filterElements s)
{
foreach (int i in this)
{
return s.Sum();//here I am tryint to get the sum of all elements in IntList s
}

}
}
class Program
{
static void Main(string[] args)
{
IntList s = new IntList(7, 9, 13, 23, 45, 1, 8, 45);
s.Act(Console.WriteLine);
s.Filter(delegate (int x) { return x % 2 == 0; }).Act(Console.WriteLine);

//anonymous method that takes an IntList x and checks if one or more values inside it are > or = with 25.If yes,then print them on the console.
filterElements f = delegate(IntList x)
{
foreach(var i in x)
{
if (i >= 25)
Console.WriteLine(i);
}
};
f(s);
var result=s.SumNumbers(delegate(int x, int y) { return x + y; }).

}```

I declare a method AddNumbers which i want to return the total sum of a list's integers but it's not working as it says that s(filterElements) does not contain a definition of Sum.I want to use the method AddNumbers to calculate the total sum of IntList "s" but whatever i do,I still get error either on the method side,either when i try to calculate it.Can anyone please help me figure it out?

Best regards//

Tuesday, October 30, 2018 11:14 PM

### All replies

• Hi,

`public delegate void filterElements(int x);`

But here your parameter is IntList:

`filterElements f = delegate (IntList x)`

And what is this:

`var result=s.SumNumbers(delegate(int x, int y) { return x + y; }).`

And this:

```        public void SumNumbers(filterElements s)
{
foreach (int i in this)
{
return s.Sum();//here I am tryint to get the sum of all elements in IntList s
}

}```

You want to return something in a void method?

Tuesday, October 30, 2018 11:56 PM
• Wednesday, October 31, 2018 12:56 PM
• Hi,

I have changed the value of the parameter from int to IntList.

`var result=s.SumNumbers(delegate(int x, int y) { return x + y; }).`
Here i am declaring a variable and i want to calculate the sum of the numbers in the list.I want to make it in such way that I should use the methods and the other delegates without any loops.Is there a possibility to do that?I also tried with Sum().I also want to use the Act() method to display the result.In the function called SumNumbers I am trying to also calculate the sum of the entire numbers in the list and then assign the method to the newly created list in the test class.
Thursday, November 1, 2018 8:58 PM
• Hi CrazyVibe,

The code in your feedback use the two parameters. But in the definition of the SunNumbers method, it define only one parameter.

Best Regards,

Wendy

MSDN Community Support
Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

Tuesday, November 6, 2018 8:47 AM