The C# IEnumerable.Count(IEnumerable<TSource>)
and IEnumerable.Count(IEnumerable<TSource>, Func<TSource,Boolean>)
both return type int
implying it can return a value less than zero. It doesn't make sense for it to do so, but if the type is int
it's theoretically possible for the result to be a negative value.
IEnumerable<string> list = GetMyList();
int listCount = list.Count();
// is it more correct to do this:
if(listCount <= 0)
{
DoSomething();
}
else
{
DoSomethingElse();
}
// or this:
if(listCount == 0)
{
DoSomething();
}
else if (listCount > 0)
{
DoSomethingElse();
}
else
{
// but this branch will never be hit
throw new Exception();
}
I can't find any information online about whether or not that can actually happen, and the Documentation for Enumerable.Count does not specify any cases in which it might.
Just wondering if anyone has any experience with this happening or any information on this.
Thanks