I'd like to use an array initializer to build one byte array out of another byte array as well as some other bytes that form a header/trailer. Basically, I'd like to do something like this:
byte[] DecorateByteArray(byte[] payload)
{
return new byte[] { 0, 1, 2, payload.GetBytes(), 3, 4, 5};
}
GetBytes()
above is fictional, unfortunately.
Is there any nice/elegant way to do this? I solved this by using a BinaryWriter
to write everything to a MemoryStream
, and then converting this into a byte array with MemoryStream.ToArray()
, but it feels kind of clunky.
One easy way is to break out each into parts and then concat them
You can create a new collection that is a
List<byte>
, but that has an overload ofAdd
that adds a whole array of bytes:This then lets you use the collection initializer for this type to supply either a single byte or a sequence of bytes, which you can then turn back into an array if you need an array:
The closest you could get would be:
That would be pretty inefficient though. You'd be better off doing something like:
(Consider using
Buffer.BlockCopy
too, where appropriate.)Then call it with: