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:
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.
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.
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.
发布评论
评论(3)
但这正是为什么它不应该是字节数组的原因。假设您这样做:
每次调用 GetByteArray 时,您都必须创建一个新的字节数组。为什么?因为有人可能已经改变了你上次分发的内容!通过分发字节数组,您可以保证每次都必须从头开始重建该字节数组。
相比之下,如果您分发只读的字节集合,那么您可以一遍又一遍地分发相同的集合。你知道它不会改变。
But that is precisely why it should not be a byte array. Suppose you do this:
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.
您已经有了答案 - FxCop 输出在大多数情况下只是有帮助的建议 - 而不是命令 - 如果这个特定的输出不适用于您,您甚至可以将其关闭。
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.
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.