0

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);

1 Answer 1

1

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);
Sign up to request clarification or add additional context in comments.

2 Comments

If ByteToInsert is an byte array, can I use for to loop through it?
@Jacobus21 if ByteToInsert is a byte[], then it is very badly named! But: InsertRange

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.