Why is List <T> .Count <= 0 worth it?

In many code snippets, I saw that the following condition is used to check for an empty list:

List<string> someList = someFunctionThatPopulatesAList();
if (someList == null || someList.Count <= 0)
    return;

I am wondering - why not use the following condition:

if (someList == null || someList.Count == 0)
    return;

Is there a case when List<T>.Countnegative?

+4
source share
3 answers

You can just try using Any () as

if ((someList!= null) && (!someList.Any())) {

}

Please note that you can use it if the list is used IEnumerable<T>, and you want to use the LINQ option.

Is there a case where List.Count is negative?

, . , , Count length, , , , , , . ( , t . .)

+5

, , .

Any() extenstion, @Rahul. List<T>, Count(), , .

  • Count, , .
  • IEnumerable, .Any() over.Count(), , .

Ref - <T> ?

+1

: Any() - .

, Length <= 0 Length Length == 0.

  • . , - , .net List < > implementation, Length(). , , , , .

  • When using code analysis tools, this will allow the code analysis tool to know what Lengthis of positive value and can improve the ability of the tool to conduct a full analysis of your code.

0
source

All Articles