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: 2020-06-22T12:38:40+05:30

1K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements