更新时间:2021-10-18 15:25:29
也许这并不直接回答你的问题,但在.NET 4.5+,我宁愿追随设计公共或受保护的API,当这些规则:
Maybe this is not directly answering your question, but in .NET 4.5+, I prefer to follow these rules when designing public or protected APIs:
的IEnumerable< T>
,如果只枚举可用; IReadOnlyCollection< T>
如果两个枚举和项目数都可用; IReadOnlyList< T>
,如果枚举,项目数和索引访问可用; 的ICollection< T>
如果枚举,项目数和修改可用; 的IList< T>
,如果枚举,项目数,索引访问和修改是可用的。IEnumerable<T>
, if only enumeration is available;IReadOnlyCollection<T>
if both enumeration and items count are available;IReadOnlyList<T>
, if enumeration, items count and indexed access are available;ICollection<T>
if enumeration, items count and modification are available;IList<T>
, if enumeration, items count, indexed access and modification are available.最后两个选项假设,这个方法不能返回数组作为的IList< T>
实施
Last two options assume, that method must not return array as IList<T>
implementation.