Concatenates the given arrays into a single array. - CSharp System

CSharp examples for System:Array Element Add

Description

Concatenates the given arrays into a single array.

Demo Code


using System.Text;
using System.Collections.Generic;
using System;/*from   www.  jav a2s. c  o  m*/

public class Main{
        /// <summary>
        /// Concatenates the given arrays into a single array.
        /// </summary>
        /// <param name="byteArrays">The arrays to concatenate</param>
        /// <returns>The concatenated resulting array.</returns>
        public static byte[] Concat(params byte[][] byteArrays)
        {
            int size = 0;
            foreach (byte[] btArray in byteArrays)
            {
                size += btArray.Length;
            }

            if (size <= 0)
            {
                return new byte[0];
            }

            byte[] result = new byte[size];
            int idx = 0;
            foreach (byte[] btArray in byteArrays)
            {
                Array.Copy(btArray, 0, result, idx, btArray.Length);
                idx += btArray.Length;
            }

            return result;
        }
}

Related Tutorials