Question

I have multiple byte lists (lines) inside another list (nested lists). How can I insert a specific byte at an specific index at an specific line?

byte ByteToInsert = 1;
int LineNumber = 2;
int IndexInSubList = 3;

// Create top-level list
List<List<byte>> NestedList = new List<List<byte>>();

// Create two nested lists
List<byte> Line1 = new List<byte> { 2, 2, 5, 25 };
List<byte> Line2 = new List<byte> { 3, 7, 8, 35 };

// Add to top-level list
NestedList.Add(Line1);
NestedList.Add(Line2);

// Insert
...

After executing the insert code, NestedLists should consist of two lines:

{ 2, 2, 5, 25 }
{ 3, 7, 8, 1, 35 }

How can I accomplish this?

Solution

thanks to Hamlet Hakobyan and Marc Gravell♦:

If a single byte is to be inserted:

NestedList[LineNumber - 1].Insert(IndexInSubList, ByteToInsert);

If a byte array is to be inserted:

NestedList[LineNumber - 1].InsertRange(IndexInSubList, BytesToInsert);
Was it helpful?

Solution

You can access to the nested list collection also by indexer. Then use Insert method to insert data at the position you need.

NestedList[LineNumber-1].Insert(IndexInSubList, ByteToInsert);
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top