且构网

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

使用IReadOnlyCollection< T>代替IEnumerable< T>用于参数以避免可能的多重枚举

更新时间:2022-04-27 15:20:43

对此有所考虑此外,根据我在问题中提到的文章,我得出的结论是,使用 IReadOnlyCollection< T> 作为参数确实可以,但仅在肯定会列举的功能。如果枚举是基于其他参数,对象状态或工作流的条件,则仍应将其作为 IEnumerable< T> 传递,以便从语义上确保惰性评估。

Having thought about this further, I have come to the conclusion, based on the article I mentioned in my Question, that it is indeed OK to use IReadOnlyCollection<T> as a parameter, but only in functions where it will definitely be enumerated. If enumeration is conditional based on other parameters, object state, or workflow, then it should still be passed in as IEnumerable<T> so that lazy evaluation is semantically ensured.