System.IO.MemoryStream我对使用基于字节数组的可写性给出 ArgumentException 的基本实验感到困惑
newBytes用文字初始化ms使用数组进行初始化,并将可写标志设置为TrueVB网
Try
Dim newBytes() As Byte = {0, 128, 255, 128, 0}
Dim ms As New System.IO.MemoryStream(newBytes, True)
ms.Write({CByte(4)}, 1, 1)
Catch ex as Exception
End Try
Run Code Online (Sandbox Code Playgroud)
C#.net
try
byte() newBytes = {0, 128, 255, 128, 0};
System.IO.MemoryStream ms = new System.IO.MemoryStream(newBytes, true);
ms.Write(byte(4), 1, 1);
catch Exception ex
end try
Run Code Online (Sandbox Code Playgroud)
异常是ArgumentException带有文本“偏移量和长度超出数组范围或计数大于从索引到源集合末尾的元素数”。
显然内存流有Length: 5并且在位置1写入一个字节应该是完全可行的,为什么会有例外?
该MemoryStream.Write方法有三个参数:
buffer- 从中写入数据的缓冲区offset- 缓冲区中从零开始的字节偏移量,从该偏移量开始将字节复制到当前流count- 写入的最大字节数请注意,第二个参数是输入数组中的偏移量,而不是输出数组中的偏移量。该MemoryStream.Position属性确定输出中的当前偏移量。
| 归档时间: |
|
| 查看次数: |
1457 次 |
| 最近记录: |