How do I check if a value in a list valid in C#?
I have a list that dynamically grows and shrinks but when the list is Empty(no values/entries in the list), I want to run alternate code.
So something like this:
if(value == "ArgumentOutOfBounds")
{
//do This
}
How do I do code that actually works in this situation?
Use the list's Count property.
if(index < List.Count)
{
(Safely work with List[index])
}
List<string> list = new List<string>();
int amountOfItemsInList = list.Count;
if (amountOfItemsInList == 0)
{
//List is empty
}
else
{
//List is not empty
}
int indexIWantFromList = 5;
if (indexIWantFromList < amountOfItemsInList)
{
//Index found in list
}
else
{
//index not found in list
}