What is the best method to add two bytes into a existing bytes array?
Should I use Array.Copy
?
What is the best method to add two bytes into a existing bytes array?
Should I use Array.Copy
?
Well, this is an interesting subject. I've made a microbenchmark and, yes, the fastest way is using Array.Copy
.
Check this out: Prepend to a C# Array
Regards
Use a List instead of a byte[]; it will provide the flexibility and it is good performance wise
List<byte> l1 = new List<byte>() { 5, 6, 7, 10, 11, 12 };
List<byte> l2 = new List<byte> { 8, 9 };
l1.InsertRange(3, l2);
Then if you need to go back to a byte[] for whatever reason you can call...
l1.ToArray();
or else
byte[] newArray = new byte[theArray.Length + 1];
theArray.CopyTo(newArray, 1);
newArray[0] = theNewByte;
theArray = newArray;