从库返回什么集合类型而不是数组?
所讨论的数组是字节流。仅返回数组的问题在于它不是只读的,因此库的客户端可以修改其内容。
包装数组的方法有很多种,我不确定该选择哪一种:
IEnumerable、IList、List、ReadOnlyCollection、Collection 等。
此外,返回类型可能与实际不同实例化类型。
我最初的方法是执行以下操作:
Data = new ReadOnlyCollection
其中 data
是字节数组。 Data 属性将是某种接口类型(IEnuerable、IList 等)。但是,我不确定使用哪个。我看到很多人推荐 IEnumerable,因为它非常标准,但这里的顺序很重要,在我看来,字节流应该保持与数组的语法相似性。 IEnumerable 不允许访问单个索引,因此它显然不是这里的最佳选择。
IList 不是只读的,所以我想 ICollection 是正确的..?真的不确定。似乎有很多集合类型,我对使用哪个有点困惑。
The array in question is a stream of bytes. The problem with returning just an array is that it is not read-only and thus the clients of the library can modify its contents.
There are so many different ways of wrapping the array I'm not sure which to choose:
IEnumerable, IList, List, ReadOnlyCollection, Collection, et cetera.
Also, the return type can be different from the actual instantiated type.
My initial approach was to do something like this:
Data = new ReadOnlyCollection<byte>(data);
Where data
is a byte array. The Data property would be of some interface type (IEnuerable, IList, et cetera.) However, I'm not sure which to use. I see many recommend IEnumerable as it is pretty standard but order matters here and a stream of bytes, in my opinion, should maintain syntactical similarities to an array. IEnumerable does not allow accessing of individual indicies so it is obviously not the optimal choice here.
IList is not readonly so I suppose ICollection would be correct..? Not sure really. There seem to be so many collection types and I'm getting a bit confused as to which to use.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
我很想返回 IList并记录该列表是不可变的。这为您提供了稍后更改实现的灵活性,但这意味着调用者不需要使用
IEnumerable
的“最低公分母”。使用
IList
作为声明类型,您仍然可以返回ReadOnlyCollection
作为实现。或者正如 Darin 提到的,您可以使用
Stream
- 特别是MemoryStream
:这将是一个只读流。如果客户端想要以流的形式读取数据(例如传递给 XML 解析器或图像加载器等),那么这将是最好的。您只需将其声明为返回
Stream
- 显式声明它返回MemoryStream
几乎没有什么好处(除非调用者想要调用ToArray,当然)。您应该记录返回的流是可读的、可查找的,但不可写的。
如果他们确实想将其视为集合,则
IList
更好。I would be tempted to return
IList<byte>
and document that the list is immutable. That provides you with a mixture of the flexiblity of changing the implementation later on, but means that callers don't need to work with the "lowest common denominator" ofIEnumerable<T>
.Using
IList<byte>
as the declaration type you can still return aReadOnlyCollection<byte>
as the implementation.Or as Darin mentions, you can use a
Stream
- in particular, aMemoryStream
:That will be a read-only stream. If clients want to read the data as a stream (e.g. to pass to an XML parser or image loader etc) then that would be best. You'd only need to declare it as returning
Stream
- there'd be very little benefit in explicitly declaring that it returnsMemoryStream
(unless the caller wants to callToArray
, of course). You should document that the returned stream is readable, seekable, but not writable.If they actually want to treat it as a collection,
IList<T>
is better.直接返回一个
Stream
而不是一个字节数组怎么样?根据您期望客户能够利用结果做什么,可能会有不同的方法。如果他们可以修改字节数组真的那么糟糕吗?即使您返回IEnumerable
,它们仍然可以调用.ToArray()
扩展方法并获取字节数组。所以我认为修改消费者的内存中的字节数组对您来说不应该是一个问题。How about directly returning a
Stream
instead of an array of bytes? Depending on what do you expect the clients to be able to do with the results there might be different approaches. Is it really so bad if they can modify the array of bytes? Even if you return anIEnumerable<T>
they can still call the.ToArray()
extension method and get an array of bytes. So I don't think that modifying an in-memory array of bytes from the consumer should be a problem for you.我建议返回一个
MemoryStream
。您可以将其构造为只读。I would suggest returning a
MemoryStream
. You can construct it so that it's read-only.