Here is a common c type function to return odd numbers from an array.
Using Predicate Delegation the above program can be simplified as
int[] numbers = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };
int[] odds = GetOdds(numbers);
private static int[] GetOdds(int[] numbers)
{
int[] odds = new int[numbers.Length];
int counter = 0;
for (int i = 0; i < numbers.Length; i++)
{
if ((numbers[i] % 2) != 0)
{
odds[counter++] = numbers[i];
}
}
return odds;
}
Powerful! Isn't it?int[] numbers = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };
int[] odds = Array.FindAll<int>(numbers, IsOdd);
private static bool IsOdd(int number)
{
return ((number % 2) != 0);
}
No comments:
Post a Comment