public class test
{
public int X{get;set;}
public int Y{get;set;}
public test(int x,int y)
{
X=x;
Y=y;
}
public override string ToString()
{
return " X is " +X+" Y is "+Y;
}
}
public static bool mybool(test obj)
{
return obj.X * obj.Y > 100;
}
test[] mytest = { new test(4, 1), new test(929, 10), new test(211, 312) ,new test(11,50)};
Predicate<test> mypredicate = mybool;
test A = Array.Find(mytest, mypredicate);
这里只找到了第一个符合条件的对象,然后程序就结束了,想问问如何才能找到全部符合条件的对象,不是找到第一个符合的就中断运行。
用FindAll,而不是Find,用错方法了
test[] A = Array.FindAll(mytest, mypredicate);
看看有没有findall函数,如果有的话前面也要用数组接收
你用LinQ中的Where不就好了吗
class Program
{
public class test
{
public int X { get; set; }
public int Y { get; set; }
public test(int x,int y)
{
X = x;
Y = y;
}
public override string ToString()
{
return " X is " + X + " Y is " + Y;
}
}
public static bool mybool(test obj)
{
return obj.X * obj.Y > 100;
}
static void Main(string[] args)
{
test[] mytest = { new test(4, 1), new test(929, 10), new test(211, 312) ,new test(11, 50) };
List<test> list = mytest.ToList().Where(item => item.X * item.Y > 100).ToList();
}
您好,我是有问必答小助手,你的问题已经有小伙伴为您解答了问题,您看下是否解决了您的问题,可以追评进行沟通哦~
如果有您比较满意的答案 / 帮您提供解决思路的答案,可以点击【采纳】按钮,给回答的小伙伴一些鼓励哦~~
ps:问答VIP仅需29元,即可享受5次/月 有问必答服务,了解详情>>>https://vip.csdn.net/askvip?utm_source=1146287632
非常感谢您使用有问必答服务,为了后续更快速的帮您解决问题,现诚邀您参与有问必答体验反馈。您的建议将会运用到我们的产品优化中,希望能得到您的支持与协助!
速戳参与调研>>>https://t.csdnimg.cn/Kf0y