CopyTo(Array) method in ArrayList

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

Introduction

CopyTo(Array) method of arraylist copies the entire ArrayList to a compatible one-dimensional Array, starting at the beginning 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");

            string[] str = new string[5];

            arr.CopyTo(str);

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

            foreach (string s in str)

            {

                Console.WriteLine(s);

            }

        }

    }

}


Output

copyto1.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.