3

I saw in many code snippets that the following condition is used to check whether a list is empty:

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

I'm wondering - why not use the following condition instead:

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

Is there any case in which List<T>.Count is negative?

Lance U. Matthews
  • 15,725
  • 6
  • 48
  • 68
Aviran Katz
  • 741
  • 2
  • 8
  • 26
  • 6
    No, there is not. you say right. It isn't necessary to use `<=` – mostafa8026 Apr 13 '16 at 10:16
  • 6
    Or better List.Any() – hendryanw Apr 13 '16 at 10:17
  • 1
    I never saw code that uses `someList.Count <= 0` and I agree that it makes no sense at all. – Toxantron Apr 13 '16 at 10:18
  • Why Any is better in this case? – Steve Apr 13 '16 at 10:18
  • @Steve Any() breaks as soon as it finds a match, while a Count iterates over the entire List to count the amount of items. – Negorath Apr 13 '16 at 10:19
  • @Hendry - *possibly*, if you believe that carries greater semantics. But it pulls in an extension method that happens to specialize for the `List` case and actually just checks whether `Count` is zero. The same code is running, you've just somewhat obscured it. – Damien_The_Unbeliever Apr 13 '16 at 10:20
  • 4
    @Negorath - no, it doesn't. `Count` is defined to be an `O(1)` operation. It's obtainable without iteration. – Damien_The_Unbeliever Apr 13 '16 at 10:20
  • @Damien_The_Unbeliever Aha, that's good to know, I was not aware of this. – Negorath Apr 13 '16 at 10:28
  • [List.Count](http://referencesource.microsoft.com/#mscorlib/system/collections/generic/list.cs,78a69d857716bc68,references) source, [Linq Any()](http://referencesource.microsoft.com/#System.Core/System/Linq/Enumerable.cs,8788153112b7ffd0) source – PTwr Apr 13 '16 at 10:33
  • 1
    Does this answer your question? [Why is List.Count a signed int? Can List.Count ever be negative?](https://stackoverflow.com/questions/69322162/why-is-listt-count-a-signed-int-can-listt-count-ever-be-negative) – Lance U. Matthews Sep 25 '21 at 22:12

4 Answers4

5

You can simply try to use Any() like

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

}

Do note that you can use it if the list uses the IEnumerable<T> and you want to use the LINQ option.

Is there any case in which List.Count is negative?

No its not. Actually whenever you are using operations like Count or length then a signed integer is returned, so it more of like an implementation to check that you are implementing it to check if it is a positive result.(However techincially you don't need to make that check. Its more of like an idea implementation.)

Rahul Tripathi
  • 168,305
  • 31
  • 280
  • 331
1

Yes you are correct, it is not necessary to use that.

You can also use Any() extenstion method as suggested by @Rahul. But because you are checking on a List<T> is it suggested to use Count(), it would be slightly faster, as the size is already known.

  • Use Count if you're using a list, since it knows it's size.
  • Use Length for an Array
  • If you just have an IEnumerable I would use .Any() over .Count() as it will be faster since it stops after checking one item.

Ref - List<T> Any or Count?

Community
  • 1
  • 1
Yogi
  • 9,174
  • 2
  • 46
  • 61
1

As others have said: Using Any() is a good solution.

There are two separate aspects why using Length <= 0 when using Length is better when compared to Length == 0.

  1. It is safer with respect to reusing the code. The code could change to use some other list class other than the standard .net List<> implementation and that other list class could under some circumstances return a negative value for Length(). Creating such a class would be a very bad idea in my opinion, but sometimes people choose to follow bad ideas, so better safe than sorry.

  2. When using code analysis tools, this would allow the code analysis tool to know that Length has a positive value and could improve the ability of the tool to make a meaningful analysis of your code.

NineBerry
  • 26,306
  • 3
  • 62
  • 93
0

Null-Conditional Operator Even the newest .NET developers are likely familiar with the NullReferenceException. This is an exception that almost always indicates a bug because the developer didn’t perform sufficient null checking before invoking a member on a (null) object.

if(someList?.Any()){
 //Something
}
Taranjit Kang
  • 2,510
  • 3
  • 20
  • 40
  • That doesn't work in current form, not for me any ways. Needs to be ----- someList?.Any() != true – TSmith Apr 08 '20 at 17:20