C# 使用字节数组的一部分
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/589099/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
using part of a byte array
提问by Jeremy
If I have an array of bytes created byte[] binBuffer = new byte[256] and I fill up 100 bytes of the array, if I want to pass only those 100 bytes to some other method, is it possible to do that without creating a new byte array of 100 bytes, copying from the old array to the new, then passing off the new array? Is there somehow I can just pass the first 100 bytes. My application specifically applies to passing the array to a stored procedure.
如果我创建了一个字节数组 byte[] binBuffer = new byte[256] 并且我填充了该数组的 100 个字节,如果我只想将这 100 个字节传递给其他方法,是否可以在不创建的情况下做到这一点一个 100 字节的新字节数组,从旧数组复制到新数组,然后传递新数组?有什么办法我可以只传递前 100 个字节。我的应用程序专门用于将数组传递给存储过程。
回答by Chris Shaffer
If you can use linq:
如果您可以使用 linq:
SomeMethod(binBuffer.Take(100));
回答by Jared
if you are using .net 3.5 you can use the Take() extension method and do the following:
如果您使用的是 .net 3.5,您可以使用 Take() 扩展方法并执行以下操作:
class Program
{
static void Main(string[] args)
{
byte[] b = new byte[1000];
dowork(b.Take(10).ToArray());
}
public static void dowork(byte[] b)
{
// do some work
}
}
回答by BFree
When an array is passed to a method, only a reference to it is actually passed, since arrays are actually reference types. You basically have a pointer to the array, and then the offset in the square brackets just tells you how many "slots" away from the start. Therefore, the only real way to do this would be to pass two parameters to your method. One being the actual array, and the second being the max number until where you need to go, in your case it would be 100. In that method you then only iterate through the array until the max number is reached.
当一个数组被传递给一个方法时,实际上只传递了一个对它的引用,因为数组实际上是引用类型。你基本上有一个指向数组的指针,然后方括号中的偏移量只是告诉你离开始有多少“槽”。因此,唯一真正的方法是将两个参数传递给您的方法。一个是实际数组,第二个是直到您需要去的地方的最大数量,在您的情况下它将是 100。在该方法中,您只需要遍历数组,直到达到最大数量。
回答by Marc Gravell
A very common pattern when working with buffers is the:
使用缓冲区时的一个非常常见的模式是:
Foo(byte[] data, int offset, int count) {...}
pattern. However, you cannot use this with a SqlCommand/ parameter, since when you assign to a parameter it consumes the entire buffer. You will need a new array:
图案。但是,您不能将它与SqlCommand/ 参数一起使用,因为当您分配给一个参数时,它会消耗整个缓冲区。您将需要一个新数组:
byte[] second = new byte[100];
Buffer.BlockCopy(first, firstOffset, second, 0, 100);
param.Value = second;
回答by Franci Penov
The short answer is: NO. Anything you do (besides passing the array itself as the parameter) will create a new array and copy the first 100 bytes in it.
最简洁的答案是不。您所做的任何事情(除了将数组本身作为参数传递)都将创建一个新数组并复制其中的前 100 个字节。
However, since you are not passing the array by value, but instead are passing a reference to it, why does it matter to you if the array is 256 bytes or 100? You are not wasting more memory. You might need to tell the method how much of that array to use, though, as the Length method will return the full length.
但是,由于您不是按值传递数组,而是传递对它的引用,为什么数组是 256 字节还是 100 字节对您来说很重要?你不会浪费更多的内存。不过,您可能需要告诉该方法要使用多少该数组,因为 Length 方法将返回完整长度。
EDIT:I just realized you want to pass the array to a stored procedure. This will copy the whole array. You will have to make a copy with only the elements you want to pass.
编辑:我刚刚意识到您想将数组传递给存储过程。这将复制整个数组。您必须仅使用要传递的元素制作副本。

