Count property vs Count() method?

后端 未结 8 2235
孤街浪徒
孤街浪徒 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 05:03

    Short Version: If you have the choice between a Count property and a Count() method always choose the property.

    The difference is mainly around the efficiency of the operation. All BCL collections which expose a Count property do so in an O(1) fashion. The Count() method though can, and often will, cost O(N). There are some checks to try and get it to O(1) for some implementations but it's by no means guaranteed.

提交回复
热议问题