Returning 'IList' vs 'ICollection' vs 'Collection'

后端 未结 6 1754
萌比男神i
萌比男神i 2020-11-28 20:17

I am confused about which collection type that I should return from my public API methods and properties.

The collections that I have in mind are IList,

6条回答
  •  不思量自难忘°
    2020-11-28 20:30

    ICollection is an interface that exposes collection semantics such as Add(), Remove(), and Count.

    Collection is a concrete implementation of the ICollection interface.

    IList is essentially an ICollection with random order-based access.

    In this case you should decide whether or not your results require list semantics such as order based indexing (then use IList) or whether you just need to return an unordered "bag" of results (then use ICollection).

提交回复
热议问题