gpt4 book ai didi

c# - FirstOrDefault 函数默认值

转载 作者:太空宇宙 更新时间:2023-11-03 18:03:56 25 4
gpt4 key购买 nike

<分区>

我有一个对象数组,我想在这个数组中找到某个特定对象的索引:

int ix = Array.IndexOf(products, products.Where(item => item != null && item.Id == "xxx").FirstOrDefault());

Id="xxx"的项目不存在,但 ix 结果为 1。所以,我猜 int 的默认值是 1。我怎么知道 1 是属于第一项还是默认值?如果我可以将默认值设置为 -1 就好了。

最后我用 findIndex 方法完成了,但想知道如何用 indexOf 方法完成。

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com