C
CrazyVibe
Guest
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))
res.Add(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);
filterElements v = s.AddNumbers;
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//
Continue reading...
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))
res.Add(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);
filterElements v = s.AddNumbers;
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//
Continue reading...