Filter an object array using delegates, without using LINQ

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:



Variant 2: Filtering a complex type array using delegate

Filtering an array of Target class on Age property.
Declaration:

   1: public static class Common
   2: {
   3:     public delegate bool ObjectFilter<TEntity>(TEntity obj, string property, int number) where TEntity : class;
   4:  
   5:     public static TEntity[] FilterObjectArray<TEntity>(TEntity[] objs, string property, ObjectFilter<TEntity> filter)
   6:         where TEntity : class
   7:     {
   8:         List<TEntity> list = new List<TEntity>();
   9:  
  10:         foreach (TEntity obj in objs)
  11:             if (filter(obj, property, 30))
  12:                 list.Add(obj);
  13:  
  14:         return list.ToArray();
  15:     }
  16: }
  17:  
  18: public class Filters
  19: {
  20:     public static bool ObjGreaterThanNumber<TEntity>(TEntity obj, string property, int number) where TEntity : class
  21:     {
  22:         return Convert.ToInt32(obj.GetType().GetProperty(property).GetValue(obj)) > number;
  23:     }
  24: }
  25:  

Target Class:
   1: public class TargetClass
   2:     {
   3:         public string Name { get; set; }
   4:         public int Age { get; set; }
   5:     }

Invocation:
   1: TargetClass[] objs = new TargetClass[]{
   2:                 new TargetClass{Name="Holmes",Age=60},
   3:                 new TargetClass{Name="David",Age=40},
   4:                 new TargetClass{Name="Dorian",Age=20}
   5:             };
   6:             
   7: Console.WriteLine("====Filtered Objects====");
   8: foreach (TargetClass tobj in Common.FilterObjectArray(objs, "Age", Filters.ObjGreaterThanNumber))
   9:     Console.WriteLine("Name: " + tobj.Name + " | Age: " + tobj.Age);

Output:


Variant 3: Filtering a complex type array using Func expression

Filter an array of Target class(refer previous example in Variant 2 for Target Class definition and objs array) using Func expression.
Declaration:

   1: public static class Common
   2: {
   3:     public static TEntity[] FilterObjectArrayUsingFuncExpression<TEntity>(TEntity[] objs, Func<TEntity,bool> func)
   4:         where TEntity : class
   5:     {
   6:         List<TEntity> list = new List<TEntity>();
   7:  
   8:         foreach (TEntity obj in objs)
   9:             if (func(obj))
  10:                 list.Add(obj);
  11:  
  12:         return list.ToArray();
  13:     }
  14: }

Invocation:
   1: Console.WriteLine("====Filtered Objects using Func expression====");
   2:  
   3: foreach (TargetClass tobj in Common.FilterObjectArrayUsingFuncExpression(objs, tc => tc.Name.Contains("i")))
   4:     Console.WriteLine("Name: " + tobj.Name + " | Age: " + tobj.Age);

Output:



Variant 4: Filtering a complex type array without any loop(Using System.Array class)

Filter an object array of Target class using a delegate and System.Array class.
Declaration:

   1: public static class Common
   2: {
   3:     public delegate bool AgeFilter<TEntity>(TEntity obj) where TEntity : class;
   4:  
   5:     public static TEntity[] FilterObjectsUsingArrayClass<TEntity>(TEntity[] objArray, AgeFilter<TEntity> filter) where TEntity : class
   6:     {
   7:         return Array.FindAll(objArray, new Predicate<TEntity>(filter));
   8:     }
   9: }
  10:  
  11: public class Filters
  12: {
  13:     public static bool AgeGreaterThan30<TEntity>(TEntity obj) where TEntity : class
  14:     {
  15:         return Convert.ToInt32(obj.GetType().GetProperty("Age").GetValue(obj)) > 30;
  16:     }
  17: }
  18:  

Invocation:

   1: Console.WriteLine("====Filtered Objects using Array Class====");
   2:  
   3: foreach (TargetClass tobj in Common.FilterObjectsUsingArrayClass(objs, Filters.AgeGreaterThan30))
   4:     Console.WriteLine("Name: " + tobj.Name + " | Age: " + tobj.Age);

Output:



As always, don’t hesitate to correct me.

-Sayan

0 comments: (+add yours?)

Post a Comment