Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to convert IEnumerable to byte array

I need to combine several arrays to one. I've found that seems to be a good way to do this:

IEnumerable<byte> Combine(byte[] a1, byte[] a2, byte[] a3)
{
    foreach (byte b in a1)
        yield return b;
    foreach (byte b in a2)
        yield return b;
    foreach (byte b in a3)
        yield return b;
}

However, I'm not well familiar with IEnumerable. How do I convert the result back to byte[] so I could work further with it?

Thank you.

like image 368
Peter Avatar asked Mar 20 '26 02:03

Peter


1 Answers

Instead of iterating them just linq's .Concat:

var joint = a1.Concat(a2).Concat(a3);

If you want to return it as an array:

joint.ToArray();
like image 59
Gilad Green Avatar answered Mar 22 '26 15:03

Gilad Green



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!