Does Index of Array Exist Does Index of Array Exist arrays arrays

Does Index of Array Exist


Test the length

int index = 25;if(index < array.Length){    //it exists}


You can use LINQ to achieve that too:

var exists = array.ElementAtOrDefault(index) != null;


What exactly do you mean by "is a valid element"? You could just do:

if (array.Length >= 26)

which would tell you whether 25 is a valid index into the array or not (assuming a 0 lower bound).

If you need to know whether it's non-null or not, just use:

if (array[25] != null)

(or a combination of the two).

If these don't help, please give a more precise meaning of "valid" for your problem.