数组索引是否存在 [英] Does Index of Array Exist
本文介绍了数组索引是否存在的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我在工作中继承了一些非常难闻的代码.我希望能找到最轻松的解决方案.
I've inherited some code at work that has a really bad smell. I'm hoping to find the most painless solution possible.
有没有办法检查某个任意数字是否是数组中的有效元素?
Is there a way to check if some arbitrary number is a valid element in an array?
示例 - 我需要检查数组 [25] 是否存在.
Example - I need to check if array[25] exists.
最好我更愿意这样做而不通过数组执行 foreach() 来找到行.
Preferably I would prefer to do this without doing a foreach() through the array to found the rows.
有什么办法可以做到这一点,还是我坚持使用 foreach 循环?
Is there any way to do this, or am I stuck with foreach loop?
推荐答案
测试长度
int index = 25;
if(index < array.Length)
{
//it exists
}
这篇关于数组索引是否存在的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文