-1

I have this an implementation of class IntList. I'm am supposed to: Use the capability of the anonymous methods to refer to a local variable in their enclosing method and the defined "Act"-method to compute the sum of an IntList’s elements (without writing any loops yourself). This is what I have done so far, but I doubt that it is correct. Any suggestions and explanations will help me here

What is my anonymous method's enclosing method in this case?

public delegate bool IntPredicate(int x);
public delegate void IntAction(int x);


class IntList : List<int>
{
    public IntList(params int[] elements) : base(elements)
    {

    }

    public void Act(IntAction f)
    {
        foreach(int i in this)
        {
            f(i);
        }
    }

    public IntList Filter(IntPredicate p)
    {
        IntList res = new IntList();
        foreach (int i in this)
        {
            if (p(i))
            {
                res.Add(i);
            }
        }
        return res;
    }

}

class Program
{
    static void Main(string[] args)
    {
        // code here
        IntList xs = new IntList();
        // adding numbers, could be random. whatever really. Here just 0..29
        for(int i =0; i<30; i++)
        {
            xs.Add(i);
        }

        int total = 0;
        xs.Act(delegate (int x)
                        {
                            total = total + x;
                            Console.WriteLine(total);
                        }
               );
        Console.ReadKey();
    }
}
Jacob Jensen
  • 15
  • 10
  • 3
    _"I doubt that it is correct. Any suggestions and explanations will help me here"_ -- that is far too broad a request for Stack Overflow. For one, you need to _know_ whether it is correct or not. If it's correct, you don't even need to post a question. If it's not correct, you need to be able to explain _exactly_ in what way it's not correct. What does the program do, and what do you want it to do instead? What _specifically_ is it that you are unable to figure out yourself? – Peter Duniho Nov 30 '19 at 22:09
  • Sure okay, but I don't know which is why I'm asking. I will re-formulate the request – Jacob Jensen Nov 30 '19 at 22:19
  • 1
    _"I don't know which"_ -- the Stack Overflow community can't provide a good answer to a question without an actual _question_. You _must_ be able to articulate exactly what the code is supposed to do as well as what it's doing now that's different from what it's supposed to do. If you cannot, then you question should (and likely will) be closed and deleted. – Peter Duniho Nov 30 '19 at 22:22
  • 1
    Summing up the elements, without writing your own Loop? Sounds like a case for LINQ. | Or it could be that the goal is for you to override the Add, Remove and Clear Functions, so you can sum up as things are added/removed. – Christopher Nov 30 '19 at 22:22

1 Answers1

1

I think this part is the "anonymous method" (because it is defined inline and doesn't have a method name):

delegate (int x)
{
    total = total + x;
    Console.WriteLine(total);
}

I think the "enclosing method" is Main().

I think the "local variable" is most likely total.

I ran your code and it seems correct to me.

robbpriestley
  • 3,050
  • 2
  • 24
  • 36