Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Func<> delegate - Clarification

When an array is given:

int[] a={1,3,4,5,67,8,899,56,12,33}

and if i wish to return the even numbers using LINQ

var q=a.where(p=>p%2==0)

If i were to use C#2.0 and strictly func<> delegate what is the way to solve it?

I tried :

Func<int, bool> func = delegate(int val) { return val % 2 == 0; };

but I am confused how to link the array "a" here.

like image 626
user160677 Avatar asked Dec 07 '22 05:12

user160677


2 Answers

int[] q = Array.FindAll<int>(a, delegate(int p) { return p % 2 == 0; });

(note this uses Predicate<int>, which is the same signature as Func<int,bool>)

like image 82
Marc Gravell Avatar answered Jan 01 '23 23:01

Marc Gravell


You can use Predicate and Array.FindAll.

Predicate<int> func = delegate(int val) { return val % 2 == 0; };

Array.FindAll<int>(a, func);
like image 43
bruno conde Avatar answered Jan 01 '23 22:01

bruno conde