如何读取字节数组中的前3个字节

Mar*_*ark 3 c#

我有字节数组,我只需要读取前3个字节而不是更多.

C#4.0

Jef*_*ado 18

这些足够吗?

IEnumerable<byte> firstThree = myArray.Take(3);
byte[] firstThreeAsArray = myArray.Take(3).ToArray();
List<byte> firstThreeAsList = myArray.Take(3).ToList();
Run Code Online (Sandbox Code Playgroud)


cjk*_*cjk 7

怎么样:

Byte byte1 = bytesInput[0];
Byte byte2 = bytesInput[1];
Byte byte3 = bytesInput[2];
Run Code Online (Sandbox Code Playgroud)

或者在数组中:

Byte[] threeBytes = new Byte[] { bytesInput[0], bytesInput[1], bytesInput[2] };
Run Code Online (Sandbox Code Playgroud)

要么:

Byte[] threeBytes = new Byte[3];
Array.Copy(bytesInput, threeBytes, 0, 3); 
     // not sure on the overload but its similar to this
Run Code Online (Sandbox Code Playgroud)