且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

您喜欢哪个接口:T [],IEnumerable< T>,IList< T>或其他?

更新时间:2022-05-15 15:31:46

我的首选项是 IEnumerable< T> 。任何其他建议的界面给出允许消费者修改底层集合的外观。这几乎肯定不是你想要做的,因为它允许消费者静默修改内部集合。

My preference is IEnumerable<T>. Any other of the suggested interfaces gives the appearance of allowing the consumer to modify the underlying collection. This is almost certainly not what you want to do as it's allowing consumers to silently modify an internal collection.

另一个很好的一个IMHO,是 ReadOnlyCollection< T> 。它允许所有有趣的.Count和Indexer属性,并明确地告诉消费者你不能修改我的数据。

Another good one IMHO, is ReadOnlyCollection<T>. It allows for all of the fun .Count and Indexer properties and unambiguously says to the consumer "you cannot modify my data".