1. 程式人生 > >C# ArrayList.ToArray() (將 ArrayList 的元素複製到新陣列中)

C# ArrayList.ToArray() (將 ArrayList 的元素複製到新陣列中)

            ArrayList oList = new ArrayList();
            ArrayList oList2 = new ArrayList();
            oList.Add("a");
            oList.Add("b");
            oList.Add("c");
            oList2.Add(1);
            oList2.Add(2);
            oList2.Add(3);

            string[] s = (string[])oList.ToArray(typeof(string));
            int[] i = (int[])oList2.ToArray(Type.GetType("System.Int32"));

            foreach (string item in s)
            {
                Console.WriteLine(item);
            }

            foreach (int item in i)
            {
                Console.WriteLine(item.ToString());
            }
            Console.ReadKey();