كيف احل هذه السوال

The purpose of this exercise is to illustrate the use of delegates and especially anonymous method expressions of the form delegate(...) { ... }.
Use the code below as a start for your exercise. Read all the text carefully and use the time necessary to understand the different problems and solutions in the exercise.
using System; using System.Collections.Generic; // Delegate types to describe predicates on ints and actions on ints. public delegate bool IntPredicate(int x); public delegate void IntAction(int x); // Integer lists with Act and Filter operations. // An IntList containing the element 7 9 13 may be constructed as // new IntList(7, 9, 13) due to the params modifier. 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 { public static void Main(String[] args) { // code here } } The code above defines the delegates:
public delegate bool IntPredicate(int x); public delegate void IntAction(int x);
The code further declares a class IntList that is a subclass of .Net’s List<int> class (which is an arraylist). Class IntList uses the delegate types in two methods that take a delegate as argument:
list.Act(f) applies delegate f to all elements of list.
list.Filter(p) creates a new IntList containing those elements x from list for which p(x) is true.
Add code to the file’s Main method that creates an IntList and calls the Act and Filter methods on that list and various anonymous delegate expressions. For instance, if xs is an IntList, you can print all its elements like this:
xs.Act(Console.WriteLine); This works because there is an overload of Console.WriteLine that takes an int argument and therefore conforms to the IntAction delegate type. You can use Filter and Act to print only the even list elements (those divisible by 2) like this: xs.Filter(delegate(int x) { return x%2==0; }).Act(Console.WriteLine); Explain what goes on above: How many IntList are there in total, including xs? Further, use anonymous methods to write an expression that prints only those list elements that are greater than or equal to 25. An anonymous method may refer to local variables in the enclosing method. Use this fact and the Act method to compute the sum of an IntList’s elements (without writing any loops yourself). Note: If you have an urge to make this exercise more complicated and exciting, you could declare a generic subclass MyList<T> of List<T> instead of IntList, and make everything work for generic lists instead of just IntLists. You need generic delegate types Predicate<T> and Action<T>, but in fact these are already declared in the .Net System namespace.