<分区>
在我从事的许多项目中,每当我必须返回只读集合时,我都会使用 IEnumerable<T>
接口(interface)并使其类型特定,如下所示:
Public ReadOnly Property GetValues() As IEnumerable(Of Integer)
Get
'code to return the values'
End Get
End Property
大多数时候,我会返回一个列表,但在某些函数和只读属性中,我会返回一个数组,该数组也可以通过扩展方法的友善礼貌来达到目的。
我的问题是 我返回 IEnumerable<T>
是否违反了任何设计原则? s 而不是特定类型(例如:List<T>
、HashSet<T>
、Stack<T>
或 Array
s)?