我有一個包含這樣資料的位元組陣列(托管代碼)
byte[] test = {0xA,0xB,0xC,0xD}; // data is much longer...
如果我明確這樣做,LINQ 將從原始陣列創建一個副本。
var test2 = test.Skip(1).Take(2).ToArray();
我需要它的一部分,但沒有創建一個副本作為新陣列。
受此啟發, 獲取一個陣列子集,而無需像在 C 中那樣使用指標進行復制
var test2 = test.Skip(1).Take(2);
它有效,但結果是 IEnumerable,我需要一個純位元組 []。
所以基本上目標是擁有一個真正的原始陣列塊(不是塊的副本),如果我修改一個元素,也反映在塊中(比如在 C 中有指標)
是否可以將 IEnumerable 轉換為 byte[] 而無需復制?
是否可以這樣做(沒有 LINQ)?
先感謝您!
uj5u.com熱心網友回復:
看來您正在尋找Span或Memory<byte>:
byte[] test = { 0xA, 0xB, 0xC, 0xD };
// Span is some kind of c-like pointer to the array
// first 2 - skip 2 items
// second 2 - take 2 items
Span<byte> span = new Span<byte>(test, 2, 2);
// We modify the array item...
test[3] = 0xFF;
// ... and span reflects the modification
// Note that we have skipped 2 items, that's why span[1], not span[3]
byte result = span[1]; // 0xFF
轉載請註明出處,本文鏈接:https://www.uj5u.com/gongcheng/526608.html
標籤:C#数组
上一篇:為什么我的串列有多余的雙括號?
