Search code examples
c#c++performancecompare

Difference between list.Count > 0 and list.Count != 0


I have a list of things. Is there any difference between list.Count > 0 and list.Count != 0? Or any performance difference in these codes?

if (list.Count > 0)
    // do some stuff

if (list.Count != 0)
    // do some stuff

note: list.Count Can't be less than ziro..


Solution

  • There's realistically no difference as the list can never have less than 0 items, but == for integral comparisons is wicked fast, so it's probably faster than >. A cooler looking approach is list.Any().

    (This is assuming by list you mean the List type or any built in IEnumerable/Collection)