LIST<T>
-
Hi guys, what is the best way to check if a LIST contain any elements. I tried both:
if(ListName.Count > 0)
{}
else
{}
//andif(ListName.Any())
{}
else
{}
but both of these give a runtime error if there is nothing(NULL) in the LIST on the if() statement. Please help. Regards Morgs...
-
Hi guys, what is the best way to check if a LIST contain any elements. I tried both:
if(ListName.Count > 0)
{}
else
{}
//andif(ListName.Any())
{}
else
{}
but both of these give a runtime error if there is nothing(NULL) in the LIST on the if() statement. Please help. Regards Morgs...
NULL and ListName.Count are two separate things. NULL means the ListName is not initialized yet (so you can't access any properties either). ListName.Count > 0 means that the ListName object is initialized, but just doesn't contain any items. What you want to have is:
if(ListName != null && ListName.Count > 0) { }
or
if(ListName != null && ListName.Any()) { } -
NULL and ListName.Count are two separate things. NULL means the ListName is not initialized yet (so you can't access any properties either). ListName.Count > 0 means that the ListName object is initialized, but just doesn't contain any items. What you want to have is:
if(ListName != null && ListName.Count > 0) { }
or
if(ListName != null && ListName.Any()) { }i have initialised the LIST like so:
LIST nxt = new LIST();
nxt = dbh.GetNextPage(cId, "1.1");
if (nxt.Count > 0)
{}
and should the LIST contain no records i get a runtime error: index was outside the bounds of the array....yak yak yak. same applies to when i use Any();
-
i have initialised the LIST like so:
LIST nxt = new LIST();
nxt = dbh.GetNextPage(cId, "1.1");
if (nxt.Count > 0)
{}
and should the LIST contain no records i get a runtime error: index was outside the bounds of the array....yak yak yak. same applies to when i use Any();