If I have a list of strings
List<String> list = new list<String>();
list.add("str1");
list.add("str2");
list.add("str3");
and I want to know if for example index position 2 contains an element, is there a simple way of doing this without counting the length of the list or using a try catch ?
As this will fail, I can get round it with a try catch, but this seems excessive
if(list.ElementAt(2) != null)
{
// logic
}
int? here = (list.ElementAtOrDefault(2) != 0 ? list[2]:(int?) null);
if (list.Count > desiredIndex && list[desiredIndex] != null)
{
// logic
}
Source: Stackoverflow.com