FxCop 说我应该返回通用列表接口而不是字节数组。我应该吗?

发布于 2024-12-09 23:06:27 字数 1431 浏览 1 评论 0原文

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(3

掀纱窥君容 2024-12-16 23:06:27

可能会操作大量字节,因此每次都必须重新复制整个列表以将其以字节数组的形式获取,这可能会减慢速度。

但这正是为什么它不应该是字节数组的原因。假设您这样做:

byte[] x1 = GetByteArray();
x1[0] = 0;
byte[] x2 = GetByteArray();

每次调用 GetByteArray 时,您都必须创建一个新的字节数组。为什么?因为有人可能已经改变了你上次分发的内容!通过分发字节数组,您可以保证每次都必须从头开始重建该字节数组。

相比之下,如果您分发只读的字节集合,那么您可以一遍又一遍地分发相同的集合。你知道它不会改变。

There is potential for large amounts of bytes to be manipulated so having to recopy the entire list just to get it in the form of a byte array each time would likely slow things down.

But that is precisely why it should not be a byte array. Suppose you do this:

byte[] x1 = GetByteArray();
x1[0] = 0;
byte[] x2 = GetByteArray();

Every time you call GetByteArray you have to create a new byte array. Why? Because someone might have changed the one you handed out last time to have different contents! By handing out a byte array you guarantee that you are going to have to reconstruct that byte array from scratch every single time.

By contrast, if you hand out a read only collection of bytes then you can hand out the same collection over and over again. You know it is not going to change.

在风中等你 2024-12-16 23:06:27

这个图书馆的客户总是希望事情能够以术语表达
字节数组,以便它们与其余部分很好地交互
框架。

您已经有了答案 - FxCop 输出在大多数情况下只是有帮助的建议 - 而不是命令 - 如果这个特定的输出不适用于您,您甚至可以将其关闭。

Clients of this library are always going to want things to be in terms
of an array of bytes so that they interface nicely with the rest of
the framework.

There you have your answer - FxCop output is in most cases just helpful suggestions - not commands - if this particular one doesn't apply to you you can even turn it off.

用心笑 2024-12-16 23:06:27

FxCop 提供的指南和建议并不总是适用于所有情况。您不需要遵循它们,在某些情况下您也不应该遵循它们。

The guidelines and recommendations offered by FxCop are not always applicable in every situation. You don't need to follow them, and in some situations you shouldn't.

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