Conversion of ArrayList to Array in C#


To convert an ArrayList to Array, use the ToArray() method in C#.

Firstly, set an ArrayList −

ArrayList arrList = new ArrayList();
arrList.Add("one");
arrList.Add("two");
arrList.Add("three");

Now, to convert, use the ToArray() method −

arrList.ToArray(typeof(string)) as string[];

Let us see the complete code −

Example

 Live Demo

using System;
using System.Collections;

public class Program {
   public static void Main() {
      ArrayList arrList = new ArrayList();
      arrList.Add("one");
      arrList.Add("two");
      arrList.Add("three");

      string[] arr = arrList.ToArray(typeof(string)) as string[];

      foreach (string res in arr) {
         Console.WriteLine(res);
      }
   }
}

Output

one
two
three

Updated on: 22-Jun-2020

877 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements