C# 合並兩個數組總結


 byte[] b1 = new byte[] { 1, 2, 3, 4, 5 };
            byte[] b2 = new byte[] { 6, 7, 8, 9 };
            byte[] b3 = new byte[b1.Length + b2.Length];
            char[] b4 = new char[] { '1', '2', '3', '4', '5' };
            char[] b5 = new char[] { '6', '7', '8', '9' };
            char[] b6 = new char[b1.Length + b2.Length];
            int[] b7 = new int[] { 1, 2, 3, 4, 5 };
            int[] b8 = new int[] { 6, 7, 8, 9 };
            int[] b9 = new int[b1.Length + b2.Length];
            string[] b10 = new string[] { "1", "2", "3", "4", "5" };
            string[] b11 = new string[] { "6", "7", "8", "9" };
            string[] b12 = new string[b1.Length + b2.Length];


            Buffer.BlockCopy(b1, 0, b3, 0, b1.Length);//這種方法僅適用於字節數組
            Buffer.BlockCopy(b2, 0, b3, b1.Length, b2.Length);
            b7.CopyTo(b9, 0);//這種方法適用於所有數組
            b8.CopyTo(b9, b7.Length);
            b6 = b4.Concat(b5).ToArray();//這種linq方法適用於所有數組,狠,一句話搞定
            foreach (var item in b3)
            {
                Console.Write(item + " ");
            }
            Console.WriteLine();
            foreach (var item in b6)
            {
                Console.Write(item + " ");
            }
            Console.WriteLine();
            foreach (var item in b9)
            {
                Console.Write(item + " ");
            }
            Console.ReadKey();

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM