Eoi*_*ell 129
测试长度
int index = 25;
if(index < array.Length)
{
    //it exists
}
Shi*_*mmy 78
您也可以使用LINQ实现这一目标:
var exists = array.ElementAtOrDefault(index) != null;
Jon*_*eet 17
你究竟是什么意思"是一个有效的元素"?你可以这样做:
if (array.Length >= 26)
这将告诉你25是否是数组的有效索引(假设0下限).
如果你需要知道它是否为空,只需使用:
if (array[25] != null)
(或两者的组合).
如果这些没有帮助,请为您的问题提供更准确的"有效"含义.
Mar*_*ris 10
假设您还想检查该项是否为空
if (array.Length > 25 && array[25] != null)
{
    //it exists
}
// I'd modify this slightly to be more resilient to a bad parameter
// it will handle your case and better handle other cases given to it:
int index = 25;
if (index >= 0 && index < array.Length)
{
    // Array element found
}
| 归档时间: | 
 | 
| 查看次数: | 110233 次 | 
| 最近记录: |