If i
is the index you want to have, check the Count
:
if (i >= 0 && (list.Count - 1) >= i)
{
// okay, the item is there
}
If talking about nullable types, you could also check if the item on that index isn’t null
:
if (i >= 0 && (list.Count - 1) >= i && list[i] != null)
{
// okay, the item is there, and it has a value
}
0
solved How to check if there is an element at the specific index in a list?