Count property vs Count() method?

后端 未结 8 2234
孤街浪徒
孤街浪徒 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:16

    The .Count() method might be smart enough, or know about the type in question, and if so, it might use the underlying .Count property.

    Then again, it might not.

    I would say it is safe to assume that if the collection has a .Count property itself, that's going to be your best bet when it comes to performance.

    If the .Count() method doesn't know about the collection, it will enumerate over it, which will be an O(n) operation.

提交回复
热议问题