Filter an object array using delegates, without using LINQ

0 comments

Note: Thank you for visiting my blog. However, this blog is not maintained actively anymore. This post is now also in my new blog. Feel free to leave a comment there.

Variant 1: Filtering a .net simple type array using delegate

Using delegate filter an array of Integers. Delegate takes to arguments, one is the value from the array and the second one is the number, the array member with value greater than this number will be returned(filtered).
Declaration:

   1: public static class Common
   2:     {
   3:         public delegate bool IntFilter(int value, int number);
   4:  
   5:         public static int[] FilterArrayOfInts(int[] ints, IntFilter filter)
   6:         {
   7:             ArrayList aList = new ArrayList();
   8:             foreach (int i in ints)
   9:                 if (filter(i, 30))
  10:                     aList.Add(i);
  11:             return (int[])aList.ToArray(typeof(int));
  12:         }
  13:  
  14:     }
  15:  
  16: public class Filters
  17: {
  18:     public static bool GreaterThanNumber(int value, int number)
  19:     {
  20:         return value >= number;
  21:     }
  22: }

Invocation:
   1: int[] targetArray = { 10, 20, 30, 60, 50, 25 };
   2: int[] filteredArray = Common.FilterArrayOfInts(targetArray, Filters.GreaterThanNumber);
   3: Console.WriteLine("====Filtered Array====");
   4: foreach (int num in filteredArray)
   5:     Console.WriteLine(num);

Output: