Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Check if Array Position Exists

Tags:

arrays

c#

list

I am trying to check if the position of an array exists or not.

I am trying to output positions 1, 2, 3, 4 and 5 string values from a list. Where the list is less than 5, it needs to display a '-' as the string value.

For example, a list of 3 should display: Value, Value, Value, -, -

I cannot however work out how to check this, and I keep getting index was out of range errors.

I have tried:

if (String.IsNullOrEmpty(formGuideCount[3]))
{
    game4 = formGuideCount[3];
}
else
{
    game4 = "-";
}

Can anyone tell me what I should be using to check if that position does not exist?

Thanks

like image 471
JackofAll Avatar asked Dec 12 '22 16:12

JackofAll


1 Answers

You can use the Array.Length of array to validate the index location exists.

if(formGuideCount.Length > 3)
{
      game4 = formGuideCount[3];
}
like image 113
Adil Avatar answered Dec 14 '22 05:12

Adil