Calling the Common Library Filter Method : Filter « LINQ « C# / C Sharp






Calling the Common Library Filter Method

 

using System.Collections;
using System;

public delegate bool IntFilter(int i);

public class MainClass {
    public static void Main() {
        int[] nums = { 1, 2, 3, 4, 5, 6};
        int[] oddNums = FilterArrayOfInts(nums, IsOdd);
        foreach (int i in oddNums)
            Console.WriteLine(i);
    }
    public static bool IsOdd(int i) {
        return ((i & 1) == 1);
    }
    public static int[] FilterArrayOfInts(int[] ints, IntFilter filter) {
        ArrayList aList = new ArrayList();
        foreach (int i in ints) {
            if (filter(i)) {
                aList.Add(i);
            }
        }
        return ((int[])aList.ToArray(typeof(int)));
    }
}

 








Related examples in the same category

1.Use delegate to create a filter
2.Calling the Filter Method with an Anonymous Method
3.Calling the Filter Method with a Lambda Expression