从库返回什么集合类型而不是数组?

发布于 2024-12-09 05:13:20 字数 541 浏览 1 评论 0原文

所讨论的数组是字节流。仅返回数组的问题在于它不是只读的,因此库的客户端可以修改其内容。

包装数组的方法有很多种,我不确定该选择哪一种:

IEnumerable、IList、List、ReadOnlyCollection、Collection 等。

此外,返回类型可能与实际不同实例化类型。

我最初的方法是执行以下操作:

Data = new ReadOnlyCollection(data);

其中 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

原来是傀儡 2024-12-16 05:13:20

我很想返回 IList并记录该列表是不可变的。这为您提供了稍后更改实现的灵活性,但这意味着调用者不需要使用 IEnumerable 的“最低公分母”。

使用 IList 作为声明类型,您仍然可以返回 ReadOnlyCollection 作为实现。

或者正如 Darin 提到的,您可以使用 Stream - 特别是 MemoryStream

return new MemoryStream(data, false);

这将是一个只读流。如果客户端想要以流的形式读取数据(例如传递给 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" of IEnumerable<T>.

Using IList<byte> as the declaration type you can still return a ReadOnlyCollection<byte> as the implementation.

Or as Darin mentions, you can use a Stream - in particular, a MemoryStream:

return new MemoryStream(data, false);

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 returns MemoryStream (unless the caller wants to call ToArray, 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.

内心激荡 2024-12-16 05:13:20

直接返回一个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 an IEnumerable<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.

淡淡的优雅 2024-12-16 05:13:20

我建议返回一个MemoryStream。您可以将其构造为只读。

I would suggest returning a MemoryStream. You can construct it so that it's read-only.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文