Count property vs Count() method?

后端 未结 8 2218
孤街浪徒
孤街浪徒 2020-11-27 04:21

Working with a collection I have the two ways of getting the count of objects; Count (the property) and Count() (the method). Does anyone know what

8条回答
  •  轻奢々
    轻奢々 (楼主)
    2020-11-27 04:56

    Count() method is an extension method that iterates each element of an IEnumerable<> and returns how many elements are there. If the instance of IEnumerable is actually a List<>, so it's optimized to return the Count property instead of iterating all elements.

提交回复
热议问题