Calling the Filter Method with a Lambda Expression : Filter « LINQ « C# / CSharp Tutorial






using System;
using System.Collections;

public delegate bool IntFilter(int i);

public class MainClass {
    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)));
    }
    public static void Main() {
        int[] nums = { 1, 2, 3, 4, 5};
        int[] oddNums = FilterArrayOfInts(nums, i => ((i & 1) == 1));
        foreach (int i in oddNums)
            Console.WriteLine(i);
    }
}








22.41.Filter
22.41.1.Use delegate to create a filter
22.41.2.Calling the Common Library Filter Method
22.41.3.Calling the Filter Method with an Anonymous Method
22.41.4.Calling the Filter Method with a Lambda Expression