开发者

Func<> delegate - Clarification

开发者 https://www.devze.com 2022-12-10 21:13 出处:网络
When an array is given: int[] a={1,3,4,5,67,8,899,56开发者_如何学Python,12,33} and if i wish to return the even numbers using LINQ

When an array is given:

int[] a={1,3,4,5,67,8,899,56开发者_如何学Python,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.


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>)


You can use Predicate and Array.FindAll.

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

Array.FindAll<int>(a, func);
0

精彩评论

暂无评论...
验证码 换一张
取 消