CopyTo(Array,Int32) method in ArrayList

In this article I will discuss the CopyTo(Array,Int32) method of ArrayList.
  • 2232

Introduction

Copies the entire ArrayList to a compatible one-dimensional Array, starting at the specified index of the target array.In the following example I will explain the use of CopyTo method ,In this I will copy the entire arraylist into an array.This article also shows that how we convert the ArrayList to Array.

Example

namespace ConsoleApplication3

{

    class Program

    {

        static void Main(string[] args)

        {

            ArrayList arr = new ArrayList();

            arr.Add("Red");

            arr.Add("Yellow");

            arr.Add("Green");

            arr.Add("Brown");

            arr.Add("Pink");

            Console.Write("{");

            Console.WriteLine("The arraylist is as:");

            foreach (string s in arr)

            {

                Console.Write(s + ",");

            }

            Console.Write("}");

            Console.WriteLine();

            Console.WriteLine();

            string[] str = new string[10];

            str[0] = "White";

            str[1] = "Black";

            str[2] = "Orange";

            str[3] = "Gray";

            str[4] = "Olive";

            Console.WriteLine("The string array is as:");

            Console.Write("{");

            foreach (string s1 in str)

            {

                Console.Write(s1 + ",");

            }

            Console.Write("}");

            Console.WriteLine();

            Console.WriteLine();

            arr.CopyTo(str, 5);

            Console.WriteLine("The copied array is as:");

            Console.Write("{");

            foreach (string s in str)

            {

                Console.Write(s + ",");

            }

            Console.Write("}");

            Console.WriteLine();

            Console.WriteLine();

        }

    }

}


Output

copyto2.jpg
 

Ask Your Question
 
Got a programming related question? You may want to post your question here
 

Categories

More Articles

© 2020 DotNetHeaven. All rights reserved.